如何判断PHP表单中哪个按钮被单击?

时间:2022-11-15 23:27:37

I have several buttons on my page, but I'm not sure how to tell which one was clicked. Here's the markup for my two buttons:

我的页面上有几个按钮,但我不知道如何判断哪个按钮被点击了。这是我的两个按钮的标记:

<input type="submit" id="btnSubmit" value="Save Changes" />
<input type="submit" id="btnDelete" value="Delete" />

3 个解决方案

#1


114  

With an html form like:

使用html表单:

<input type="submit" name="btnSubmit" value="Save Changes" />
<input type="submit" name="btnDelete" value="Delete" />

The php code to use would look like:

要使用的php代码如下:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    //something posted

    if (isset($_POST['btnDelete'])) {
        // btnDelete
    } else {
        //assume btnSubmit
    }
}

You should always assume or default to the first submit button to appear in the form html source code. In practice, the various browsers reliably send the name/value of a submit button with the post data when:

您应该始终假设或默认为第一个提交按钮,以出现在表单html源代码中。在实践中,当:

  1. The user literally clicks the submit button with the mouse or pointing device
  2. 用户通过鼠标或指向设备点击提交按钮
  3. Or there is focus on the submit button(they tabbed to it), and then the enter key is pressed.
  4. 或者关注提交按钮(它们对其进行选项卡),然后按下enter键。

Other ways to submit a form exist, and some browsers/versions decide not to send the name/value of any submit buttons in some of these situations. For example, many users submit forms by pressing the enter key when the cursor/focus is on a text field. Forms can also be submitted via javascript, as well as some more obscure methods.

还存在其他提交表单的方法,有些浏览器/版本决定在某些情况下不发送任何提交按钮的名称/值。例如,当光标/焦点在文本字段上时,许多用户通过按enter键提交表单。表单也可以通过javascript以及一些更模糊的方法提交。

It's important to pay attention to this detail, otherwise your can really frustrate your users when they submit a form, yet "nothing happens" and their data is lost because your code failed to detect a form submission because you did not anticipate the fact that the name/value of a submit button may not be sent with the post data.

重要的是要注意这个细节,否则你真的可以阻挠你的用户提交表单时,然而,“什么也没有发生”和他们的数据丢失,因为代码未能发现表单提交,因为你没有预料到这样一个事实:提交按钮的名称/值可能不是发送post数据。

Also, the above advice should be used for forms with a single submit button too because you should always assume a default submit button.

同样,上面的建议也应该用于只有一个提交按钮的表单,因为您应该始终假设一个默认的提交按钮。

I'm aware that the internet is filled with tons of form-handler tutorials, and almost of all them do nothing more than check for the name and value of a submit button. But, they're just plain wrong!

我知道internet上充斥着大量的表单处理程序教程,几乎所有这些教程都只是检查提交按钮的名称和值。但是,他们完全错了!

#2


8  

In HTML:

在HTML中:

<input type="submit" id="btnSubmit" name="btnSubmit" value="Save Changes" />
<input type="submit" id="btnDelete" name="btnDelete" value="Delete" />

In PHP:

在PHP中:

if (isset($_POST["btnSubmit"])){
  // "Save Changes" clicked
} else if (isset($_POST["btnDelete"])){
  // "Delete" clicked
}

#3


-2  

Are you asking in php or javascript.

你是用php还是用javascript ?

If it is in php, give the name of that and use the post or get method, after that you can use the option of isset or that particular button name is checked to that value.

如果它在php中,请给出它的名称并使用post或get方法,然后您可以使用isset选项或将该特定按钮名称检查到该值。

If it is in js, use getElementById for that

如果是在js中,则使用getElementById

#1


114  

With an html form like:

使用html表单:

<input type="submit" name="btnSubmit" value="Save Changes" />
<input type="submit" name="btnDelete" value="Delete" />

The php code to use would look like:

要使用的php代码如下:

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    //something posted

    if (isset($_POST['btnDelete'])) {
        // btnDelete
    } else {
        //assume btnSubmit
    }
}

You should always assume or default to the first submit button to appear in the form html source code. In practice, the various browsers reliably send the name/value of a submit button with the post data when:

您应该始终假设或默认为第一个提交按钮,以出现在表单html源代码中。在实践中,当:

  1. The user literally clicks the submit button with the mouse or pointing device
  2. 用户通过鼠标或指向设备点击提交按钮
  3. Or there is focus on the submit button(they tabbed to it), and then the enter key is pressed.
  4. 或者关注提交按钮(它们对其进行选项卡),然后按下enter键。

Other ways to submit a form exist, and some browsers/versions decide not to send the name/value of any submit buttons in some of these situations. For example, many users submit forms by pressing the enter key when the cursor/focus is on a text field. Forms can also be submitted via javascript, as well as some more obscure methods.

还存在其他提交表单的方法,有些浏览器/版本决定在某些情况下不发送任何提交按钮的名称/值。例如,当光标/焦点在文本字段上时,许多用户通过按enter键提交表单。表单也可以通过javascript以及一些更模糊的方法提交。

It's important to pay attention to this detail, otherwise your can really frustrate your users when they submit a form, yet "nothing happens" and their data is lost because your code failed to detect a form submission because you did not anticipate the fact that the name/value of a submit button may not be sent with the post data.

重要的是要注意这个细节,否则你真的可以阻挠你的用户提交表单时,然而,“什么也没有发生”和他们的数据丢失,因为代码未能发现表单提交,因为你没有预料到这样一个事实:提交按钮的名称/值可能不是发送post数据。

Also, the above advice should be used for forms with a single submit button too because you should always assume a default submit button.

同样,上面的建议也应该用于只有一个提交按钮的表单,因为您应该始终假设一个默认的提交按钮。

I'm aware that the internet is filled with tons of form-handler tutorials, and almost of all them do nothing more than check for the name and value of a submit button. But, they're just plain wrong!

我知道internet上充斥着大量的表单处理程序教程,几乎所有这些教程都只是检查提交按钮的名称和值。但是,他们完全错了!

#2


8  

In HTML:

在HTML中:

<input type="submit" id="btnSubmit" name="btnSubmit" value="Save Changes" />
<input type="submit" id="btnDelete" name="btnDelete" value="Delete" />

In PHP:

在PHP中:

if (isset($_POST["btnSubmit"])){
  // "Save Changes" clicked
} else if (isset($_POST["btnDelete"])){
  // "Delete" clicked
}

#3


-2  

Are you asking in php or javascript.

你是用php还是用javascript ?

If it is in php, give the name of that and use the post or get method, after that you can use the option of isset or that particular button name is checked to that value.

如果它在php中,请给出它的名称并使用post或get方法,然后您可以使用isset选项或将该特定按钮名称检查到该值。

If it is in js, use getElementById for that

如果是在js中,则使用getElementById