<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();
4
Answers
In order of preference
you can use AJAX with JQuery $.post() method for submitting both forms simultaneously.
This would be my approach. Use jquery ajax to define a
.submit()
function for each form (the procedure to follow when submitted). Use.click()
to "click" both submit buttons programmatically. Then usereturn false
to prevent page refresh. This should submit both forms simultaneously. I was unable to test this without the php actions.