Arturo Arturo - 1 year ago 98
jQuery Question

Form with two submit buttons

I have a form with two submit buttons as per below.

<form id="choice" name='form' method="POST" action="/">
<button type="submit" name="vote" id="btnMinus" value="1"></button>
<button type="submit" name="vote" id="btnPlus" value="2"></button>

I have this code in order to prevent that the form is not submitted twice. However, it gives a "bad request" error. Seems that it doesn't send the "vote" value. Any ideas?


Answer Source

Following @RoryMcCrossan's comment, you can use a hidden input and set it's value once the button is clicked:

$('#choice button').click(function(e) {  
<script src=""></script>
<form id="choice" name='form' method="POST" action="/">
  <input id="vote-el" type="hidden" name="vote" value="" />
  <button type="submit" name="vote" id="btnMinus" value="1">1</button>
  <button type="submit" name="vote" id="btnPlus" value="2">2</button>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download