1

Keep in mind that the user does NOT have to click the submit nput. They could tab over to it and push enter.

So considering all ways a form can be submitted, how can you determine which one was used to submit the form inside the submit event. I have different names on the two submit elements.

Joe
  • 7,922
  • 18
  • 54
  • 83
  • 3
    create a hidden input. change the value before submit. – Jonathan Ong Mar 30 '12 at 17:59
  • I know it feels guilty to make a simple comment into an answer, Jonathan, but this to me is a great answer! You might as well get your just desserts for it. ;-) – Greg Pettit Mar 30 '12 at 18:02
  • 2
    I would add that it is possible to give each submit button a unique name (it should already have a value for displaying text), and then you can check which variable exists to determine which form was clicked without using hidden inputs. – Katie Kilian Mar 30 '12 at 18:15

2 Answers2

4

Any action that triggers a submit button — be it an actual mouse click or some keyboard action — still triggers a "click" event and still causes the input to be included as a form parameter.

That is, if you see a form parameter with your input field's name and value, you know that that submit button was clicked.

edit — if the form submit happens because you hit "Enter" in a text field, the browser picks the first submit button (I think; that seems to be what Firefox does at least). (Wait; scratch that; Firefox seems to find the next "submit" input after the element that had focus when "Enter" was pressed ...)

Thus:

<form action='whatever' method='post'>
  <input type='text' name='text'>
  <input name='submit1' value='submit1' type='submit'>
  <input name='submit2' value='submit2' type='submit'>
</form>

Hitting "Enter" in the text field would result in "submit1=submit1" being a form parameter, as would hitting "Enter" when "submit1" had focus. Tabbing to "submit2" and hitting "Enter" would result in "submit2=submit2" being among the parameters.

In either case, only one of the "submit" inputs shows up in the parameter list.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • +1, but a snippet with some comments will make all clearer =) – Prusse Mar 30 '12 at 18:16
  • And both the "submit"s could have the same "name" and different "value"s. – Prusse Mar 30 '12 at 18:37
  • How do you see what form parameter was submitted in the submit method? – Joe Mar 30 '12 at 19:07
  • You "see" them by testing on the server. How do you handle *any* parameters on the server side? Your server code should know that it should expect different "submit" buttons, so it can either tell which was pressed by looking for a particular parameter name, or else you can give all the buttons the same name and different *values* so the server just checks the value. – Pointy Mar 30 '12 at 21:06
1

Instead of using different names, use different values, e.g:

<input type="submit" name="submit" value="Submit A">
<input type="submit" name="submit" value="Submit B">

You can also use buttons instead of inputs if you'd like the labels to be different from the values.

imsky
  • 3,239
  • 17
  • 16