The value of my submit button is being lost if submitted via javascript in Chrome v19. It works in IE8, IE9 and FF12. I have the following page that exhibits the problem:
<html>
<head>
<script type="text/javascript">
function submitorder()
{
document.openord.TYPE.value='Submitted using javascript';
document.openord.submit();
}
</script>
</head>
<body>
<pre>
The contents of $_REQUEST are:
<?php var_dump($_REQUEST); ?>
</pre>
<form method="post" action="showParms.php" name="openord">
<input type="hidden" name="TYPE" value="Submitted the regular ol' way">
<input type="hidden" name="NAME" value="Frederick Q. Larson">
<input type="submit" name="TASK" value="Submit with Javascript" onclick='submitorder();'>
<input type="submit" name="TASK" value="Normal Submit">
</form>
</body>
</html>
Click the “Normal Submit” and you’ll see a value for TASK like this:
The contents of $_REQUEST are:
array(3) {
["TYPE"]=>
string(29) "Submitted the regular ol' way"
["NAME"]=>
string(19) "Frederick Q. Larson"
["TASK"]=>
string(13) "Normal Submit"
}
Not so with the “Submit with Javascript” button:
The contents of $_REQUEST are:
array(2) {
["TYPE"]=>
string(26) "Submitted using javascript"
["NAME"]=>
string(19) "Frederick Q. Larson"
}
In IE and FF, when clicking “Submit with Javascript” you see:
The contents of $_REQUEST are:
array(3) {
["TYPE"]=>
string(26) "Submitted using javascript"
["NAME"]=>
string(19) "Frederick Q. Larson"
["TASK"]=>
string(22) "Submit with Javascript"
}
Any idea why Chrome is having a problem with this?
If all you want is to submit the form with a parameter changed through JavaScript, then
is sufficient, because the form will automatically be submitted when the user clicks on the submit button.
“Correct” behaviour
According to the Specification, section “4.10.22.4 Constructing the form data set”:
This algorithm is used when a form is submitted (specification reference).
Apparently, since no button was clicked, the submitter is empty, hence no submit element.
In Firefox, the button which initiates the function which submits the form seems to be flagged submitter. Calling the function without a (form submit) button shows the same behaviour as in Chrome: http://jsfiddle.net/3fwcr/1/.
When
event.preventDefault();is added to the button’sclickevent, then the form also submits as in Chrome: http://jsfiddle.net/3fwcr/2/.Apparently, Firefox’ default submission is still triggered, even though the form is already being submitted (seems to be against the specification: “If form is already being submitted (..), then abort these steps” (“these steps” = form submission)).