0

I have following forms:

<form name="google" method="post" action="http://google.com/"></form>
<form name="yahoo" method="post" action="http://yahoo.com" target="_blank"></form>

Is there any chance to submit these two forms with one single mouse click, e.g. with one submit button? jQuery solution isn't acceptable since the website I have doesn't use it so to load external lib for one single operation isn't reasonable.

Any chance to make it through pure Javascript?

Thanks in advance!

Alex F
  • 183
  • 2
  • 14
  • - http://stackoverflow.com/questions/19785198/two-forms-one-submit-button - http://stackoverflow.com/questions/7843355/submit-two-forms-with-one-button – mdt0093 Aug 14 '14 at 11:43
  • you can call a js function on click of submit, then you can manually fire SUBMIT event on both the forms. i think this will work – mulla.azzi Aug 14 '14 at 11:44
  • @mulla.azzi no, that does not work if it fires a POST. After the post the page is reloaded and the information of the second form will be lost. You need to safe the information from the second form before sending one – Fonzy Aug 14 '14 at 11:46
  • Yes, I can use AJAX. Any sample of AJAX request? All I managed to find are written with jQuery – Alex F Aug 14 '14 at 11:56
  • 1
    add `target="_blank"` to both of your forms and they should open and post in a new window. – Darren Wainwright Aug 14 '14 at 11:56
  • @Darren that might actually work. nice one – Fonzy Aug 14 '14 at 11:59
  • Just submit `target="_blank"` form first – Livon Aug 14 '14 at 12:09
  • Nope, target="_blank" won't work since html doesn't allow submission of two forms simultaneously. What I'm trying to find is kind of hack, unique solution. – Alex F Aug 14 '14 at 13:10

2 Answers2

2

Sadly, afaik the simple answer is no since if you submit one form the page reloads and post the $_POST to the server. Can't you combine the two forms under one tag? HTML5 can do this. You can also use hidden fields and read the second form and supply the first forms hidden fields with the information before sending.

Hope that helps, Cheers!

Fonzy
  • 691
  • 3
  • 14
  • I can't combine because I need to open two different urls, one is my website and the second one is external one. – Alex F Aug 14 '14 at 11:54
  • @AlexF then AJAX would be your way to go, although it's a clumsy way since you are sending data to two different, external, sources. Normally AJAX only works on your own domain due to the "Same origin policy" see here: http://stackoverflow.com/questions/4613310/how-to-call-external-url-in-jquery – Fonzy Aug 14 '14 at 11:56
1

html:

<form id="form1" name="google" method="post" action="http://google.com/" onsubmit="submitBoth(); return false;"></form>
<form id="form2" name="yahoo" method="post" action="http://yahoo.com" target="_blank" onsubmit="submitBoth(); return false;"></form>

js:

function submitBoth() {
  document.getElementById('form1').submit();
  document.getElementById('form2').submit();
}
Livon
  • 436
  • 1
  • 3
  • 10