如何从HTML到PHP中获取所有复选框变量?

时间:2022-12-13 11:03:14

I noticed that PHP seems to return only values of checked checkboxes. I would like to see a list of checkboxes, not just values of checked checkboxes. Is there a way to detect variables of unchecked boxes?

我注意到PHP似乎只返回选中的复选框的值。我希望看到一个复选框列表,而不仅仅是选中的复选框的值。是否有一种方法可以检测未选中的复选框的变量?

I asked because I want to be able to update settings. For example, I have a few options that are already checked but if an user decides to uncheck an option, I need to know that unchecked value so I can update the option to be disabled.

我问这个问题是因为我想要更新设置。例如,我有一些选项已经被选中,但是如果用户决定取消选中某个选项,我需要知道这个未选中的值,以便更新要禁用的选项。

5 个解决方案

#1


58  

I just ran into this problem myself. I solved it by adding a duplicate hidden field with the same name. When the browser sends this information, the second field overrides the first (so ensure that the hidden field comes first).

我自己也遇到了这个问题。我通过添加一个同名的重复隐藏字段来解决这个问题。当浏览器发送这些信息时,第二个字段会覆盖第一个字段(所以要确保隐藏字段优先)。

<input type="hidden" name="foo" value="">
<input type="checkbox" name="foo" value="bar">

If the checkbox is not checked you get:

如果没有选中复选框,您将得到:

$_REQUEST[ 'foo' ] == ""

If the checkbox is checked you get:

如果选中复选框,您将得到:

$_REQUEST[ 'foo' ] == "bar"

#2


2  

This isn't something that can be done purely in PHP.

这不是完全可以用PHP完成的。

Browsers only send information about checkboxes if they are checked, if you want to also send information about unchecked checkboxes, you'll have to add hidden fields in the form and use javascript to manage them.

浏览器只在选中复选框时才会发送有关复选框的信息,如果还想发送关于未选中复选框的信息,就必须在表单中添加隐藏字段,并使用javascript来管理它们。

#3


1  

I just stumbled across this problem myself and I sorted it by updating all values in the database to unchecked then re-checking only the ones that are in the POST data, this works fine for me but might not be everyone's cup of tea.

我自己刚刚偶然发现了这个问题,我将数据库中的所有值更新为unchecked,然后只检查POST数据中的值,这对我来说很好,但可能不是每个人都喜欢的。

#4


0  

A pure PHP implementation doesn't seem possible, you can try using jQuery/AJAX though.

纯PHP实现似乎不太可能,但您可以尝试使用jQuery/AJAX。

#5


0  

Suppose you have a 3 checkboxes you want to check, and update_settings is the name of your functions that take the checkbox name as a first argument and a bool value as a second one (activate or not).

假设有3个复选框要选中,update_settings是将复选框名称作为第一个参数的函数名,而bool值作为第二个参数(激活与否)的函数名。

Take the following snippet:

把以下代码片段:

$checkboxes = array("checkbox1", "checkbox2", "checkbox3");
foreach($checkboxes as $checkbox){
    $checked = isset($_POST[$checkbox]);
    update_settings($checkbox, $checked);
}

Althouth Peter Kovacs solution it's going to work, I don't think it's practical since you can already check your variables using isset.

虽然Peter Kovacs的解决方案是可行的,但是我不认为它是可行的,因为您已经可以使用isset检查变量了。

#1


58  

I just ran into this problem myself. I solved it by adding a duplicate hidden field with the same name. When the browser sends this information, the second field overrides the first (so ensure that the hidden field comes first).

我自己也遇到了这个问题。我通过添加一个同名的重复隐藏字段来解决这个问题。当浏览器发送这些信息时,第二个字段会覆盖第一个字段(所以要确保隐藏字段优先)。

<input type="hidden" name="foo" value="">
<input type="checkbox" name="foo" value="bar">

If the checkbox is not checked you get:

如果没有选中复选框,您将得到:

$_REQUEST[ 'foo' ] == ""

If the checkbox is checked you get:

如果选中复选框,您将得到:

$_REQUEST[ 'foo' ] == "bar"

#2


2  

This isn't something that can be done purely in PHP.

这不是完全可以用PHP完成的。

Browsers only send information about checkboxes if they are checked, if you want to also send information about unchecked checkboxes, you'll have to add hidden fields in the form and use javascript to manage them.

浏览器只在选中复选框时才会发送有关复选框的信息,如果还想发送关于未选中复选框的信息,就必须在表单中添加隐藏字段,并使用javascript来管理它们。

#3


1  

I just stumbled across this problem myself and I sorted it by updating all values in the database to unchecked then re-checking only the ones that are in the POST data, this works fine for me but might not be everyone's cup of tea.

我自己刚刚偶然发现了这个问题,我将数据库中的所有值更新为unchecked,然后只检查POST数据中的值,这对我来说很好,但可能不是每个人都喜欢的。

#4


0  

A pure PHP implementation doesn't seem possible, you can try using jQuery/AJAX though.

纯PHP实现似乎不太可能,但您可以尝试使用jQuery/AJAX。

#5


0  

Suppose you have a 3 checkboxes you want to check, and update_settings is the name of your functions that take the checkbox name as a first argument and a bool value as a second one (activate or not).

假设有3个复选框要选中,update_settings是将复选框名称作为第一个参数的函数名,而bool值作为第二个参数(激活与否)的函数名。

Take the following snippet:

把以下代码片段:

$checkboxes = array("checkbox1", "checkbox2", "checkbox3");
foreach($checkboxes as $checkbox){
    $checked = isset($_POST[$checkbox]);
    update_settings($checkbox, $checked);
}

Althouth Peter Kovacs solution it's going to work, I don't think it's practical since you can already check your variables using isset.

虽然Peter Kovacs的解决方案是可行的,但是我不认为它是可行的,因为您已经可以使用isset检查变量了。