即使没有从 HTML 到 PHP 检查,如何获取所有复选框变量?

发布于 2024-08-15 05:09:12 字数 151 浏览 1 评论 0原文

我注意到 PHP 似乎只返回选中复选框的值。我想查看复选框列表,而不仅仅是选中复选框的值。有没有办法检测未选中框的变量?

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

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

海夕 2024-08-22 05:09:12

我自己刚刚遇到了这个问题。我通过添加一个重复的同名隐藏字段解决了这个问题。当浏览器发送此信息时,第二个字段将覆盖第一个字段(因此请确保 hidden 字段位于第一个)。

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

如果未选中 checkbox,您将得到:

$_REQUEST[ 'foo' ] == ""

如果选中 checkbox,您将得到:

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

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"
┾廆蒐ゝ 2024-08-22 05:09:12

这不是纯粹用 PHP 可以完成的事情。

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

This isn't something that can be done purely in 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.

冷清清 2024-08-22 05:09:12

我自己偶然发现了这个问题,我通过将数据库中的所有值更新为未选中然后仅重新检查 POST 数据中的值来对其进行排序,这对我来说很好,但可能不是每个人都喜欢。

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.

违心° 2024-08-22 05:09:12

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

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

黎歌 2024-08-22 05:09:12

假设您要检查 3 个复选框,update_settings 是函数的名称,该函数将复选框名称作为第一个参数,将 bool 值作为第二个参数(激活或不激活)。

采取以下片段:

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

尽管 Peter Kovacs 解决方案它会起作用,我认为它不实用,因为你已经可以使用 isset 检查你的变量。

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).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文