If a form is submitted but not by any specific button, such as
- by pressing Enter
- using
HTMLFormElement.submit()
in JS
how is a browser supposed to determine which of multiple submit buttons, if any, to use as the one pressed?
This is significant on two levels:
- calling an
onclick
event handler attached to a submit button - the data sent back to the web server
My experiments so far have shown that:
- when pressing Enter, Firefox, Opera and Safari use the first submit button in the form
- when pressing Enter, IE uses either the first submit button or none at all depending on conditions I haven't been able to figure out
- all these browsers use none at all for a JS submit
What does the standard say?
If it would help, here's my test code (the PHP is relevant only to my method of testing, not to my question itself)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
</head>
<body>
<h1>Get</h1>
<dl>
<?php foreach ($_GET as $k => $v) echo "<dt>$k</dt><dd>$v</dd>"; ?>
</dl>
<h1>Post</h1>
<dl>
<?php foreach ($_POST as $k => $v) echo "<dt>$k</dt><dd>$v</dd>"; ?>
</dl>
<form name="theForm" method="<?php echo isset($_GET['method']) ? $_GET['method'] : 'get'; ?>" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<input type="text" name="method" />
<input type="submit" name="action" value="Button 1" onclick="alert('Button 1'); return true" />
<input type="text" name="stuff" />
<input type="submit" name="action" value="Button 2" onclick="alert('Button 2'); return true" />
<input type="button" value="submit" onclick="document.theForm.submit();" />
</form>
</body></html>
When you have multiple submit buttons in a single form and a user presses the ENTER key to submit the form from a text field, this code overrides default functionality, by calling the submit event on the form from the key press event. Here is that code:
I think this post would help if someone wants to do it with jQuery:
http://greatwebguy.com/programming/dom/default-html-button-submit-on-enter-with-jquery/
The basic solution is:
and another I liked was:
I had a form with 11 submit buttons on it, and it would always use the first submit button when the user pressed enter. I read elsewhere that it is not a good idea (bad practice) to have more than one submit button on a form, and the best way to do this is have the button you want as default, as the only submit button on the form. The other buttons should be made into "TYPE=BUTTON" and an onClick event added that calls your own submit routine in Javascript. Something like this :-
Here, button3 is the default, and although you are programmatically submitting the form with the other buttons, the mjsubmit routine validates them. HTH.
I struggled with the same question since i had submit button in the middle of the from which redirected submit to another page, like so:
When user pressed enter key, this button was clicked instead of another submit button.
So i did some primitive tests by creating a from with multiple submit buttons and different visibility options and onclick event alerting which button was clicked: https://jsfiddle.net/aqfy51om/1/
Browsers and OS'es i used for testing:
WINDOWS
OSX
Most of these browsers clicked very first button despite the visibility options applied exept IE and Safari which clicked the third button, which is "visible" inside "hidden" container:
So my suggestion, which i'm going to use myself, is:
If you form has multiple submit buttons with different meaning, then include submit button with default action at the beginning of the form which is either:
style="width: 0; height: 0; overflow: hidden;"
EDIT Another option might be to offset the button(still at the beginning of the from)
style="position: absolute; left: -9999px; top: -9999px;"
, just tried it in IE - worked , but i have no idea what else it can screw up, for example printing..Another solution I've used is to just have one button in the form, and fake the other buttons.
Here's an example:
I then style the span elements to look like a button. A JS listener observes the span and performs the desired operation once clicked.
Not necessarily right for all situations, but at least it's pretty easy to do.
From the HTML 4 spec:
This means that given more than 1 submit button and none activated (clicked), none should be successful.
And I'd argue this makes sense: Imagine a huge form with multiple submit-buttons. At the top, there is a "delete this record"-button, then lots of inputs follow and at the bottom there is an "update this record"-button. A user hitting enter while in a field at the bottom of the form would never suspect that he implicitly hits the "delete this record" from the top.
Therefore I think it is not a good idea to use the first or any other button it the user does not define (click) one. Nevertheless, browsers are doing it of course.