PHP isset() 返回 false 什么时候应该返回 true?

发布于 2024-12-22 03:59:59 字数 813 浏览 1 评论 0原文

好吧...所以在我的表单上,我将所有字段设置为如下所示:

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 技术交流群。

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

发布评论

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

评论(2

迟月 2024-12-29 03:59:59

从 HTML 中的输入名称中删除单引号,使其显示为:

name="formdata[active]"

添加单引号意味着您必须以以下方式访问 PHP 中的数组:

$_POST['formdata']['\'active\'']

或者

$_POST['formdata']["'active'"]

这是非常不方便的。

Remove the single quotes from the input names in your HTML so it reads:

name="formdata[active]"

Adding the single quotes would mean you would have to access the array in PHP as:

$_POST['formdata']['\'active\'']

or

$_POST['formdata']["'active'"]

which is highly inconvenient.

遇见了你 2024-12-29 03:59:59

缺少括号:

if (!isset($_POST['formdata']['active'])) echo "Error 1";
if (empty($_POST['formdata']['active'])) echo "Error 2";

missing parenthesis:

if (!isset($_POST['formdata']['active'])) echo "Error 1";
if (empty($_POST['formdata']['active'])) echo "Error 2";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文