Fun with multiple submit buttons | 35 Comments
Posted in Browsers on 8th December 2005, 12:58 am by Stuart
In a project that I am working on at the moment I have several forms that need to have more than one submit button. It’s a process set out across several pages with a form on each page. In order to navigate between the pages I decided I would like to use submits for both the “Next” and the “Back” buttons. With Safari enforcing the button lookfrom OSX it’s preferable to use submits otherwise there will be inconsistency if you use a mixture of styled links and submit buttons.
The plan was to use two submit buttons and depending on which one is pressed, a different action takes place. To discern which button had been pressed I was going to look at the value of the buttons to tell them apart. Sounds simple and straight forward but in reality there some issues to overcome:
1. Variations when using Enter to submit in IE
I remembered that my colleague Tim had mentioned a while back that he had experienced an issue with IE not sending the name/value pair for the submit button when Enter was pressed after filling in a field in the form.
Tim knocked up a quick test script so that we could see what happens with IE. After testing we found that IE doesn’t send the name/value pair for the submit button if you press enter without focussing on the submit button first. However after some further testing of my own I found that this behaviour only happens if one text input is used. If you use two or more text inputs, the name/value pair of the submit button highest in the source is sent (more on that below). From my tests this seems to be the same for IE 5-6.0 and IE 5.2 Mac.
To allow for this ‘feature’ you either need to make sure you always use two text fields (the second can be hidden using CSS) or Tim’s suggestion was to use a switch statement in the script that processes the form that has a default action if no submit value is received with the rest of the fields sent.
I also noticed further curiosities in other browsers when using enter to submit a form: (Bear in mind you can’t submit using enter if focused on a textarea for obvious reasons!)
- Opera (v8.51 at time of testing) doesn’t allow submissions using enter if the focus is not on a text field or the submit itself.
- Firefox (v1.5 at time of testing) doesn’t allow submissions using enter if the focus is on a select
- Safari (v2.02 at time of testing) doesn’t allow submissions using enter if the focus is on a radio button (enter activates the radio button)
2. The order of submits in the source
If you fill in a field and hit ‘enter’ you will see that the name/value pair of the first submit button (as it appears in the markup) is sent (excluding the special case with IE above). This is a logical thing to do though it does mean when using multiple submit buttons you do need to worry about where in markup the submit buttons are placed.
In my example I want ‘Next’ to be the default ‘action’ so I have to make sure that the ‘Next’ button is higher in the source than the ‘Back’ button. For this to work I have used CSS to position the ‘Next’ button. When viewed with author stylesheet off you will see that the ‘Next’ button appears higher as I have used a <br /> to separate the buttons onto separate lines. This is a small point but to see the ‘Next’ and ‘Back’ buttons the wrong way round is confusing.
Try the examples
To view the test scripts click on one of the links below:
This certainly isn’t the most interesting post in the world but if you worry about these small details then you won’t put your users in a position where an unexpected action takes place when dealing with forms with more than one submit button.

Pingback: Einsichten » Spaß mit der Enter-Taste
Pingback: Byte-Dompteur » mehrere submits in einem Form
Pingback: Mehrere Submit-Buttons – hangy.de‽
Pingback: Mehrere Submit-Buttons in einem Formular - XHTMLforum