即使没有从 HTML 到 PHP 进行检查,我如何获取所有复选框变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1944019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how do I get all checkbox variables even if not checked from HTML to PHP?
提问by netrox
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.
我问是因为我希望能够更新设置。例如,我有一些已经选中的选项,但如果用户决定取消选中某个选项,我需要知道未选中的值,以便我可以更新要禁用的选项。
回答by Peter Kovacs
I just ran into this problem myself. I solved it by adding a duplicate hiddenfield with the same name. When the browser sends this information, the second field overrides the first (so ensure that the hiddenfield comes first).
我自己刚刚遇到了这个问题。我通过添加一个hidden同名的重复字段来解决它。当浏览器发送此信息时,第二个字段会覆盖第一个字段(因此请确保该hidden字段在前)。
<input type="hidden" name="foo" value="">
<input type="checkbox" name="foo" value="bar">
If the checkboxis not checked you get:
如果checkbox未选中,您会得到:
$_REQUEST[ 'foo' ] == ""
If the checkboxis checked you get:
如果checkbox被选中,你会得到:
$_REQUEST[ 'foo' ] == "bar"
回答by Ben S
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 来管理它们。
回答by R Brill
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.
我只是自己偶然发现了这个问题,我通过将数据库中的所有值更新为未选中然后仅重新检查 POST 数据中的值来对其进行排序,这对我来说很好用,但可能不是每个人都喜欢喝茶。
回答by Alix Axel
A pure PHP implementation doesn't seem possible, you can try using jQuery/AJAX though.
纯 PHP 实现似乎不太可能,不过您可以尝试使用 jQuery/AJAX。
回答by GmonC
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 Kovacssolution it's going to work, I don't think it's practical since you can already check your variables using isset.
Althouth Peter Kovacs解决方案它会起作用,我认为它不实用,因为您已经可以使用 isset 检查您的变量。

