Skip to content Skip to sidebar Skip to footer

Submitting Two Forms With A Single Button

I want to submit two forms with a single submit button. Anyone know how to do it?

Solution 1:

$('#form1').submit(function() {
   $('#form2').submit();
});

However, this probably will only submit one of them (unless they are submitted with XHR).

You can loop through the other form's input elements and append them to your other form on submit.

Solution 2:

You can do it with combine data from Form1 and Form2.

HTML Code:

<form method="post"id="myForm" action="example.php">
   <input type="text" value="Testing" name="var1">
   <input type="submit" name="submit" value="Submit">
</form>

<form method="post"id="myForm2" action="example2.php">
  <input type="text" value="Testing2" name="var2">      
</form>

Jquery Code:

$('#myForm').submit( function(){    
    url= $('#myForm').attr("action");
    data= $('#myForm').serialize();
    data2= $('#myForm2').serialize();
    $.ajax({
    type: "POST",
    url: url,
    data: data + '&' + data2, // Combine data from myForm and myForm2 using & charactersuccess: function(data){
        alert(data);
        }
    });
    returnfalse;
});

Post a Comment for "Submitting Two Forms With A Single Button"