PHP isset() 返回 false 什么时候应该返回 true?
好吧...所以在我的表单上,我将所有字段设置为如下所示:
name="formdata['name']"
和 name="formdata['active'] “
。
当然,这意味着在这些字段中输入的任何内容都应存储在 $_POST['formdata']['name']
和 $_POST['formdata']['active'] 中
。
我这样做是因为我将“formdata”传递给了我编写的几个函数(它们应该正常工作)。现在假设我正在使用它来编辑一个项目或添加一个新项目——名称将按其应有的方式执行这些功能,并且该项目将以其名称保存,但“活动”永远不会按其应有的方式保存。输入字段是一个复选框——如果选中,则值为“1”。
如果我在输入“Name”并检查“active”后 print_r($_POST['formdata'])
,我会得到: Array ( ['name'] => Name ['活动'] => 1)
。
看起来不错,对吧?但是当我执行以下操作时:
if (!isset($_POST['formdata']['active']) echo "Error 1";
if (empty($_POST['formdata']['active']) echo "Error 2";
if ($_POST['formdata']['active'] != 1) echo "Error 3";
它们都返回错误!我对此感到困惑。我是否忽略了一些非常简单的事情?我已经想了至少两个小时了。
Alright... so on my forms, I am setting all the fields to something like this:
name="formdata['name']"
and name="formdata['active']"
.
Of course, that means whatever is entered in those fields should be stored in $_POST['formdata']['name']
and $_POST['formdata']['active']
.
I do my values that way because I pass the 'formdata' to a couple of functions I have written (which work as they should.) Now let's say I'm using this to edit an item or add a new item -- the name will go through those functions as it should and the item will save with its name, but 'active' will never save as it should. The entry field is a checkbox -- if it's checked, the value is "1".
If I print_r($_POST['formdata'])
after entering 'Name' and checking 'active', I get this: Array ( ['name'] => Name ['active'] => 1 )
.
Looks fine, right? But when I do the following:
if (!isset($_POST['formdata']['active']) echo "Error 1";
if (empty($_POST['formdata']['active']) echo "Error 2";
if ($_POST['formdata']['active'] != 1) echo "Error 3";
They all return errors! I am baffled by this. Am I overlooking something very simple? I have thought about this for at least 2 hours now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 HTML 中的输入名称中删除单引号,使其显示为:
添加单引号意味着您必须以以下方式访问 PHP 中的数组:
或者
这是非常不方便的。
Remove the single quotes from the input names in your HTML so it reads:
Adding the single quotes would mean you would have to access the array in PHP as:
or
which is highly inconvenient.
缺少括号:
missing parenthesis: