I have the following web form:
<form action="process.php" method="POST">
<input type="checkbox" name="cb" onclick="submit()">
<input type="submit" name="search" value="Search">
</form>
So either clicking the submit button or the checkbox this form is submitted to process.php
page. Unexpectedly for me submit button's value "Search" is sent only when button is explicitly clicked, not when checkbox is clicked. I expected that submitting the form with submit() command triggered by onclick event will send all form's parameters as well (including submit button's "search" parameter).
So in PHP code I cannot use:
if(isset($_POST['search'])
to test if form was submitted, I have to use:
if($_SERVER['REQUEST_METHOD']=='POST')
Is this normal behaviour of submitt button?
Yes this the correct behavior. The value of a submit input is only send when activated or clicked, since here you submit the form through the function it's logical.
Check this example:
<form action="edit.php" method="post">
<input type="text" name="name">
<input type="submit" name="action" value="Save">
<input type="submit" name="action" value="Preview">
</form>
This behavior allow multiple submit action in a form.
__
On checkbox click simulate a click on the submit button instead of using submit()