Reputation: 6447
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?
Upvotes: 0
Views: 2511
Reputation: 2166
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()
Upvotes: 2
Reputation: 4519
Yes it's normal. I have made some code for you to make it work:). Add a class to your checkbox and submit button. And add the js code to your html or separate js file.
<form action="processor.php" method="post">
<input type="checkbox" class="checkbox" name="cb" onclick="submit()">
<input type="submit" class="sub" name="search" value="Search">
</form>
It goes in this function when you click on the checkbox, it is seeing when its enabled and if it's so simulate a click on the submit button.
$(".checkbox").change(function() {
if(this.checked) {
$(".sub").click();
}
});
Upvotes: 0