Implementing the jQuery Form Plugin (Multiple Submit Buttons)
Hello,
I'm totally new to javascript and jQuery. After going through a couple of tutorials, I was able to come up with a code for my purpose. The problem is that it has a variable number of submit buttons and clicking on any of the submit buttons posts only the first form.
After some research, I found out that it can easily be avoided using the jQuery Form plugin (or serialize or something), but I wasn't successful at implementing it.
This is the original code:
- $(function() {
- $(".button").click(function() {
- var journal = $("input#journal").val();
- var title = $("input#title").val();
- var authors = $("input#authors").val();
- var year = $("input#year").val();
- var location = $("input#location").val();
- var keywords = $("input#keywords").val();
-
- var dataString = 'journal=' + journal + '&title=' + title + '&year=' + year + '&authors=' + authors + '&location=' + location + '&keywords=' + keywords;
-
- $.ajax({
- type: "POST",
- url: "update.php",
- data: dataString,
- success: function() {
- alert(dataString);
- }
- });
- return false;
- });
- });
And this is the HTML:
- foreach(glob($dir) as $file) {
- $pattern = '/(.+)\/(.+)\.(pdf|PDF)/';
- if(preg_match($pattern, $file, $filename)) {
- $i = $i+1;
- echo "$i $filename[2].$filename[3]" . '
- <form name="add" id="add" method = "post" action="">
- <fieldset>
- <input type="text" name="journal" id="journal" value="" class="text-input" />
- <input type="text" name="title" id="title" value="" class="text-input" />
- <input type="text" name="authors" id="authors" value="" class="text-input" />
- <input type="number" name="year" id="year" value="" class="text-input" />
- <input type="hidden" name="location" id="location" value="'.$file.'" class="text-input" />
- <input type="text" name="keywords" id="keywords" value="" class="text-input" />
- <br />
- <input type="submit" name="submit" class="button" id="submit_btn" value="Update Database" />
- </fieldset>
- </form>';
- }
Please help me implement jQuery Forms.
Thanks,
- Pranav