Multiple submit buttons in an HTML form
Assign distinct name
and value
for each button to differentiate the submit buttons at server-side.
Server-side (e.g., PHP):
This approach simplifies server-side handling and clearly differentiates the submit buttons.
Prioritize the submission order
Visually, the "Next" button in a wizard interface might be floating right, but in HTML, arrange it first. This makes it the prime candidate when pressing the Enter key:
Leverage CSS to nail the "Next" button to the right without the aid of JavaScript:
A hidden gem for consistent 'Enter' key behavior
When styling imposes severe restrictions, the hidden input comes to the rescue:
The Enter key submits the form using the covert "Default" input, assuring consistent behavior across all browsers.
Call the shots with 'default' button attribute
To explicitly set the primary action, HTML5 introduces the formaction
attribute which can overwrite the form’s action
attribute:
This fosters flexibility in form design and explicit designation of primary actions.
Server-side distinguishing of actions
Don't forget to validate the value of the submit button on the server side. It controls both form navigation and action handling:
This strategic decision influences the form workflow thus aligning with user preferences and application needs.
When non-submit buttons are on board
For non-submission buttons that execute client-side scripts, use <input type="button">
:
This lets the button run the JavaScript code without submitting the form.
Handy tips and potential pitfalls
- Use
type="submit"
for buttons that need to submit the form. - Rely on the server-side to distinguish between actions; always validate user input.
- Floats are useful tools in controlling visual designs sans JavaScript; ensure they're cleared adequately.
- Maintain browser consistency for the default submit action if CSS is used for visually reordering buttons.
- For accessibility: Favor ordering elements correctly in HTML over excessive use of
tabindex
to ease keyboard traversal.
Was this article helpful?