Akila
Akila

Reputation: 1

Submitting two forms with a single button

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

Upvotes: 0

Views: 1800

Answers (2)

Joko Wandiro
Joko Wandiro

Reputation: 1987

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 & character
    success: function(data){
        alert(data);
        }
    });
    return false;
});

Upvotes: 0

alex
alex

Reputation: 490253

$('#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.

Upvotes: 2

Related Questions