Reputation: 301
Is it possible to submit form multiple times with different actions from HTML. i.e.
document.forms[0].action = "action1.do"; //It insert the data into database and redirect to other page in same tab.
document.forms[0].submit();
document.forms[0].action = "action2"; // Its generate a report with data inserted by action1 in new tab.
document.forms[0].target = "_blank";
document.forms[0].submit();
Form are submitting twice but the problem is second one is submitting in between the first one completion.
Please help how can i make sure that the second form submission will happen only after the first one completed.
Upvotes: 0
Views: 1533
Reputation: 9926
Submitting is asynchronous: if you do a submit, JS will continue to run and the browser performs the operation in the background. (But it makes a copy of the form so yes, you're safe to do what you did.) Now if you want to WAIT for the first operation to complete and THEN submit the second action, you have a few options, of which the most elegant is az AJAX call, using jQuery, something like:
var formData = $("form").serializeArray();
$.post("http://whatever.xxx", formData, function() {
//
// here's your callback!
// you want do the second submit here.
//
});
In the callback function of the $.post() call, you make the other submit. Problem solved.
Upvotes: 0
Reputation: 41188
You need to send the form submit as an ajax request and disable the form when you do. Once the request responds you then in the callback from that enable the form/send the next request/etc.
Upvotes: 1