views:

549

answers:

6

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?

+8  A: 

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.

kgiannakakis
@kgiannakakis how to serialize them? how do i do that? any suggestions plz.
Sarfraz
@kgiannakakis is it possible without using jquery, i mean through javascript or more precisely without ajax?
Sarfraz
THe example kgiannakakis has posted has nothing to do with Ajax.
Pekka
@Sarfraz Ahmed: Yes it is possible without jquery, but the code will be much more complicated. If you don't want to use $.post you could use the submit method of a form for example.
kgiannakakis
ok thanks for your explanation kgiannakakis :)
Sarfraz
@kgiannakakis but i think if use submit method of form, then if i want to append the serialized data, i will have to used get method or how to use submit method of form with two forms serialized?
Sarfraz
@kgiannakakis without using get method but post method should be used.
Sarfraz
@kgiannakakis i have done it with the solution you suggested. thanks
Sarfraz
+1  A: 

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.

RickNZ
+1  A: 

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.

Patrick
+1  A: 

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?

Gausie
+1  A: 

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

James Wiseman
+1  A: 

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.

Robert Neville