Submitting 2 Forms At The Same Time Using Javascript
Solution 1:
Not the prettiest way of doing it, but I thought it would be nice to share it anyways. I ended up solving it by using setTimeout
:
var requestDelay = 2000;
document.getElementById("form-a").submit();
setTimeout(function() {
document.getElementById("form-b").submit();
}, requestDelay);
This way, it waits until form A
(probably, with an estimation of 2 seconds) has been submitted, and then submits form B
. You might need to play around with the requestDelay
to fit your needs.
Solution 2:
You can't do it this way. When a form is submitted, the whole page is "destroyed" and the form's response loaded as a new page.
JavaScript code runs single-threaded so basically you're triggering the submit event on two forms at once and only when your JS is done, the browser takes over again and tries to perform the task queue you gave to it.
So if you would like to submit both forms "normally" in a non-AJAX way, you'd have to:
- Store the values of the second form on the client somehow (cookie, localstorage, you name it)
- Submit the first form which results in a page that looks exactly the same only that somewhere in the content there is some trigger for your JS code to see that the first form already was submitted.
- Restore the values to the second form (server doesn't know those because they haven't got submitted)
- Submit the second form.
Solution 3:
Form submit is a synchronous call to the server (HttpRequest). Hence, the second form can not be submitted. You are expecting to submit multiple forms without depending one on the other asynchronously. So, API like AJAX will solve the issue, but you are not looking for that. If your design allows you to keep multiple frames/ iframes in your page, keep those forms in different frames/ iframes. With this approach, you can do submit multiple forms from the parent page. If you can explain the requirement for submitting multiple forms at a time, and your limitations, you may get a better response.
Solution 4:
we can achieve with both syn/asyn way, please refer below link with sample example.... i hope this will help you.
http://yogendrakrsingh.blogspot.in/2010/03/javascript-trick-submitting-multiple.html
or
with help of serialize over third hidden for mwe can achieve. this was already posted, please below link
Post a Comment for "Submitting 2 Forms At The Same Time Using Javascript"