开发者

Submitting two forms with a single button

I want to submit two forms with a single submit bu开发者_StackOverflow社区tton. Anyone know how to do it?


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


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;
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜