Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If an HTML form has two <input type="submit"> buttons, how do I know which got clicked?

Tags:

html

forms

submit

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?

like image 743
bodacydo Avatar asked Jan 24 '10 23:01

bodacydo


People also ask

Can a HTML form have 2 submit buttons?

yes, multiple submit buttons can include in the html form. One simple example is given below.

What happens when submit button is clicked?

The form will be submitted to the server and the browser will redirect away to the current address of the browser and append as query string parameters the values of the input fields.

Can I have two or more actions in the same form?

No, a form has only one action.


1 Answers

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.

like image 178
Tim Perry Avatar answered Sep 23 '22 21:09

Tim Perry