简体   繁体   English

提交按钮值仅在单击时发送

[英]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. 因此,要么单击“提交”按钮,要么选中此表单的复选框,将其提交到process.php页面。 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). 我希望使用Submit()命令提交表单也将发送所有参数(包括提交按钮的“搜索”参数)。 So in PHP code I cannot use: 所以在PHP代码中我不能使用:

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() 在复选框上,单击模拟单击提交按钮,而不是使用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. 并将js代码添加到您的html或单独的js文件中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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