ajax form submission with jquery multiple forms

Eddas

Following the examples on this page: http://www.formget.com/submit-form-using-ajax-php-and-jquery/ I am converting the forms on one of my sites to use ajax for submission, that way uses can use the forward and back buttons, and their info isn't showing up in the address bar. It's worked so far, on pages that have a single form. But one page has multiple forms on it.

Form from campers.php

<form id="retro1457806069">
<input type="hidden" id="class" name="classa" value="retro">
<input type="hidden" id="type" name="type" value="1">
<input type="submit" id="submit" value="Rent This Camper"></form>

<form id="two1457806069">
<input type="hidden" id="class" name="classa" value="classtwo">
<input type="hidden" id="type" name="type" value="1">
<input type="submit" id="submit" value="Rent This Camper"></form>

<form id="three1457806069">
<input type="hidden" id="class" name="classa" value="classthree">
<input type="hidden" id="type" name="type" value="1">
<input type="submit" id="submit" value="Rent This Camper"></form>

Those are the actual form codes generated by my script (with all the excess text and such removed around them.

script.js:

$(document).ready(function() {
  $("#submit").click(function() {
    var classa = $("#classa").val();
    var type = $("#type").val();
    // Returns successful data submission message when the entered information is stored in database.
    var dataString = 'class=' + classa + '&type=' + type;
    if (classa == '' || type == '') {
      alert("Please Fill All Fields");
    } else {
      // AJAX Code To Submit Form.
      $.ajax({
        type: "POST",
        url: "campersub.php",
        data: dataString,
        cache: false,
        success: function(result) {
          window.location.assign("gear.php")

        }
      });
    }
    return false;
  });
});

campersub.php

<?php
session_start();
$_SESSION["class"]=$_POST["class"];
$_SESSION["type"]=$_POST["type"];
echo "Success.";
?>

The problem is, instead of adding the form information to the $_SESSION array and redirecting the browser to gear.php when I click on the first submit button, absolutely nothing happens. But when I click on the second or third submit buttons it redirects the page to campers.php?classa=classone&type=1 for example.

MikeBau

I have modified some things, having in mind mainly: 'Id attributes must be unique in the document'

(Even, If there are not other references in the code to the form elements by id then the id attribute can be removed) So I have renamed ids as they have an unique value, then I fetch the click event for catch any input submit

NOTE: I have accessed to the other elements from the parent (its form) which contains the last clicked element because I'm not sure if it is the entire HTML you have.

campers.php

<form id="retro1457806069">
<input type="hidden" id="class1" name="classa" value="retro">
<input type="hidden" id="type1" name="type" value="1">
<input type="submit" id="submit1" value="Rent This Camper"></form>

<form id="two1457806069">
<input type="hidden" id="class2" name="classa" value="classtwo">
<input type="hidden" id="type2" name="type" value="1">
<input type="submit" id="submit2" value="Rent This Camper"></form>

<form id="three1457806069">
<input type="hidden" id="class3" name="classa" value="classthree">
<input type="hidden" id="type3" name="type" value="1">
<input type="submit" id="submit3" value="Rent This Camper"></form>

script.js

$(document).ready(function() {
  $("input[type=submit]").click(function(e) {
    // **Accesing by proximty of the last clicked element, and by its name.
    e.preventDefault();
    var classa = $(this).parent('form').find('input[name="classa"]').val();
    var type = $(this).parent('form').find('input[name="type"]').val();
    // Returns successful data submission message when the entered information is stored in database.
    var dataString = 'class=' + classa + '&type=' + type;
    if (classa == '' || type == '') {
      alert("Please Fill All Fields");
    } else {
      // AJAX Code To Submit Form.
      $.ajax({
        type: "POST",
        url: "campersub.php",
        data: dataString,
        cache: false,
        success: function(result) {
          window.location.assign("gear.php")

        }
      });
    }
    return false;
  });
});

I think it must run with these changes...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery Ajax Form Submission: multiple forms - same button-id's

From Dev

django ajax multiple form submission

From Dev

Django Ajax Submission with validation and multiple forms handling

From Dev

Ajax form submission on a form created using jquery

From Dev

django ajax form submission for multiple values in for loop

From Dev

ajax multiple form submission on enter press

From Dev

Basic form submission with jquery/ajax not working

From Dev

jquery load vs ajax for form post submission

From Dev

Modal form Submission to PHP using Jquery .ajax

From Dev

CodeIgniter, Jquery Ajax, Form Submission and File Upload

From Dev

Ajax form submission with Jquery validation plugin is not working

From Dev

to submit a form among multiple forms using jquery-ajax

From Dev

to submit a form among multiple forms using jquery-ajax

From Dev

jQuery/ajax form submitting not working with multiple forms + modals not closing

From Dev

Bootstrap tab Multiple form submission by using form id in jquery

From Dev

Ajax form submission issues

From Dev

Ajax Form Validation and Submission

From Dev

generic ajax form submission

From Dev

How to process an AJAX form submission with multiple submits and Laravel routing

From Dev

multi submit buttons multi checkboxes no form jquery ajax php submission

From Dev

Chrome data compression proxy error with jquery ajax form submission

From Dev

Using jQuery AJAX to call a php page on form submission

From Dev

How to confirm a form submission with Bootbox using jQuery AJAX and JSON

From Dev

Ajax jquery form submission codeigniter doesn't work

From Dev

How do i hide a form after submission is succesfull with Ajax/Jquery

From Dev

jQuery - stop form submission from changing page using ajax

From Dev

Chrome data compression proxy error with jquery ajax form submission

From Dev

jQuery ajax form submission to php not working with response from server

From Dev

Don't reload page after form submission using ajax and jquery

Related Related

  1. 1

    Jquery Ajax Form Submission: multiple forms - same button-id's

  2. 2

    django ajax multiple form submission

  3. 3

    Django Ajax Submission with validation and multiple forms handling

  4. 4

    Ajax form submission on a form created using jquery

  5. 5

    django ajax form submission for multiple values in for loop

  6. 6

    ajax multiple form submission on enter press

  7. 7

    Basic form submission with jquery/ajax not working

  8. 8

    jquery load vs ajax for form post submission

  9. 9

    Modal form Submission to PHP using Jquery .ajax

  10. 10

    CodeIgniter, Jquery Ajax, Form Submission and File Upload

  11. 11

    Ajax form submission with Jquery validation plugin is not working

  12. 12

    to submit a form among multiple forms using jquery-ajax

  13. 13

    to submit a form among multiple forms using jquery-ajax

  14. 14

    jQuery/ajax form submitting not working with multiple forms + modals not closing

  15. 15

    Bootstrap tab Multiple form submission by using form id in jquery

  16. 16

    Ajax form submission issues

  17. 17

    Ajax Form Validation and Submission

  18. 18

    generic ajax form submission

  19. 19

    How to process an AJAX form submission with multiple submits and Laravel routing

  20. 20

    multi submit buttons multi checkboxes no form jquery ajax php submission

  21. 21

    Chrome data compression proxy error with jquery ajax form submission

  22. 22

    Using jQuery AJAX to call a php page on form submission

  23. 23

    How to confirm a form submission with Bootbox using jQuery AJAX and JSON

  24. 24

    Ajax jquery form submission codeigniter doesn't work

  25. 25

    How do i hide a form after submission is succesfull with Ajax/Jquery

  26. 26

    jQuery - stop form submission from changing page using ajax

  27. 27

    Chrome data compression proxy error with jquery ajax form submission

  28. 28

    jQuery ajax form submission to php not working with response from server

  29. 29

    Don't reload page after form submission using ajax and jquery

HotTag

Archive