Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML form, multiple submit buttons, post payload ignores non-clicked buttons: Is this standard behaviour?

Tags:

html

Consider the following form in a HTML5 document:

<form  method="post" action="http://example.com/submit/">
    <button name="confirm" value="1" type="submit">Confirm</button>
    <button name="re-send" value="1" type="submit">Re-send code</button>
    <button name="cancel" value="1" type="submit">Cancel change</button>
</form> 

Using Chrome, clicking the first button produces a request payload of confirm=1. Similarly the second button results in re-send=1 and the third cancel=1.

Examining the request payload server-side allows me to determine which of the three buttons were clicked (assuming only one of the three keys are present in the request payload).

In all cases, the key:value pairs of the non-clicked buttons are excluded from the request payload. This is very useful.

Is this behaviour (that non-clicked button key:value pairs are excluded from the request payload) standard?

like image 848
Jon Cram Avatar asked May 15 '13 09:05

Jon Cram


People also ask

What is the default behavior of a form submit?

When a user submits a form (the submit button clicked), the default action of the form is to submit the form's data to a URL that processes the data. Form elements have the action and method attributes which specify the URL to submit the form to and the type of request ( get , post , and so on), respectively.

Can HTML form have multiple submit buttons?

yes, multiple submit buttons can include in the html form.

How do you handle multiple submit buttons?

Create another button with type submit. Also add a 'formaction' attribute to this button and give it the value of the secondary URL where you want to send the form-data when this button is clicked. The formaction attribute will override the action attribute of the form and send the data to your desired location.

Why submit button is not working in HTML?

Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.


1 Answers

As far as I'm aware, yes.

From HTML4 spec: http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.13.2

Every successful control has its control name paired with its current value as part of the submitted form data set. ... If a form contains more than one submit button, only the activated submit button is successful.

From HTML5 spec: http://www.w3.org/TR/2012/CR-html5-20121217/forms.html#the-button-element

Note: A button (and its value) is only included in the form submission if the button itself was used to initiate the form submission.

I'm pretty sure < IE8 will submit the element contents instead of value (e.g. confirm=Confirm) but most other common browsers should work correctly.

Perhaps consider:

<form  method="post" action="http://example.com/submit/">
    <button name="submit-action" value="confirm" type="submit">Confirm</button>
    <button name="submit-action" value="re-send code" type="submit">Re-send code</button>
    <button name="submit-action" value="cancel change" type="submit">Cancel change</button>
</form>

Also, it's worth noting that some older browsers may have scenerios where they might treat it as though no button was clicked (e.g. form submitted by javascript or enter button instead). Might be worth catering for this by defaulting to "confirm". HTML5 on modern browsers shouldn't give you any grief, though.

like image 150
Akaoni Avatar answered Oct 24 '22 09:10

Akaoni