简体   繁体   中英

SUBMIT button value sent only when clicked

I have the following web form:

<form action="processor.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. But submit's value named "search" is sent only when button clicked explicitly, not when checkbox is clicked. Unexpectedly for me. I expected that submitting the form with submit() command will send all 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()

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();
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM