Is to possible to submit two forms simultaneously?

Is to possible to submit two forms simultaneously with either javascript or a submit button?

Form structure is ok something like this:

<form name="form1" method="post">
.........
</form>

<form name="form2" method="post">
.........
</form>

And get the data from the two in a array?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

No, this is not possible. You can create a third hidden form, that will serialize fields from both of them.

If you can use jQuery:

var str1 = $("form1").serialize();
var str2 = $("form2").serialize();
$.post(url, str1 + "&" + str2);

You need to make sure that str1 and str2 aren’t empty and of course avoid name conflicts between the two forms.

Method 2

Submiting the two form will trigger two call to the posting URL so you will not be able to get the two data at the same time.

Method 3

The purpose of the <form> tag is to encapsulate the information that the browser sends to the server.

The only way to work around that would be to have some JavaScript that walks through the DOM and pulls info from one form and puts it in the other when the user clicks Submit.

Method 4

The problem is that submitting a form changes the URL of the page. This means that when one form is submitted, the second form no longer exists in the active page, and cna therefore not be submitted.

You’ll need to submit them over AJAX. Do you use any javascript libraries?

Method 5

No, its not possible on a single page.

AJAX submission could be the aswer, as intimated elsewhere, but another possible solution could be to embed the second form with in another page and put that page within an IFrame on the first page.

Not sure how that would pan out in reality, but may be worth a go try

Method 6

No, this is not possible.

Back-up a second and tell us what you’re trying to accomplish and why you feel two forms are necessary. There may be a more elegant solution.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x