views:

324

answers:

4

Hello everyone!

Suppose I have the following HTML form:

<form>
...
<input type="submit" name="queue" value="Queue item">
<input type="submit" name="submit" value="Submit item">
</form>

How do I know which button the user clicked (without using javascript)?

I looked at submitted data and it seems that when "Queue Item" is clicked then "queue" = "Queue Item" gets sent to the server. And when "Submit item" is clicked then "submit" = "Submit item" sets sent.

Can I rely on this behavior? Is it documented somewhere in the standard on HTML forms? How do you guys do it?

Please share your experience!

Thanks, Boda Cydo.

+2  A: 

You can rely on this behavior. You get the value of the input. I would use javascript to toggle a hidden form value, but since you mentioned no javascript you do not have multiple choices.

It's a standard. Since it's an input tag, and has a value, that means you get the value submitted.

Pentium10
Thank you for the answer!
bodacydo
+3  A: 

Yep you can rely on that behaviour.

When <input type="submit" name="queue" value="Queue item"> is clicked, the field "queue" will be set and "submit" will not be.

Whereas when the other gets clicked, the field "submit" will be set, and "queue" will not be.

If you're not assured by this, you can split them into 2 forms and work on it that way.

thephpdeveloper
Thank you for the answer!
bodacydo
+11  A: 

Yes, you can rely on this; it's fully documented here. The specific relevant lines say:

'When a form is submitted for processing, some controls have their name paired with their current value and these pairs are submitted with the form. Those controls for which name/value pairs are submitted are called successful controls.'

and

'If a form contains more than one submit button, only the activated submit button is successful.'

PimTerry
Thank you for the answer and pointing to the standard!
bodacydo
When the user hits the enter button instead of pressing a button, the first submit button on a form will be the active button.
Scharrels
Great note, Scharrels!
bodacydo
No, you can't actually rely on this behavior. In IE6 you will get the value of BOTH buttons submitted - just because its the standard does not mean its implemented properly.
Erik
Sh*t. What do I do then, Erik?
bodacydo
According to http://www.vancelucas.com/blog/ie6-and-multiple-button-submit-elements/, multiple input type='submit' (like you're using) are actually handled correctly in IE, and it's just button type='submit' that are not. Unfortunately I can't get at IE from here to test, so I can't be sure on that.Other useful reading: http://muffinresearch.co.uk/archives/2005/12/08/fun-with-multiple-submit-buttons/. Apparently IE doesn't quite work like you'd expect with the enter button either. Sigh.
PimTerry
I just learned something new; i always use `<button type="submit">` rather then `<input type='submit'>` -- apparently IE6 will handle it properly with the later, just not the former, which was what made me comment.
Erik
A: 

Split the form into two forms, replicating any other inputs needed by the other action. Or, if you really just need to know if the user wants to "queue vs. submit" the item, change both submit buttons to radio selections to toggle between the two options, and have a new, separate "submit the form" button.

In that situation if you want a one-click option, you could use Javascript to detect when one of the radio buttons is selected, and auto-submit the form instantly. (Using Javascript for user interface, rather than form handling)

MidnightLightning