如何获取所有的复选框变量,即使没有从HTML到PHP检查? [英] how do I get all checkbox variables even if not checked from HTML to PHP?

查看:159
本文介绍了如何获取所有的复选框变量,即使没有从HTML到PHP检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,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.

推荐答案

我刚刚碰到这个问题。我通过添加具有相同名称的重复的 hidden 字段来解决它。当浏览器发送此信息时,第二个字段覆盖第一个字段(因此请确保隐藏字段先到)。

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">

如果未选中复选框

$_REQUEST[ 'foo' ] == ""

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

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

这篇关于如何获取所有的复选框变量,即使没有从HTML到PHP检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆