Foreach 复选框不返回值

发布于 2024-10-05 02:35:10 字数 908 浏览 6 评论 0原文

我之前写过一个脚本,在 PHP 5.2 中运行得很好。但是当最近访问我朋友的服务器(PHP 4.4.9 版本)时,我注意到一些操作没有按应有的方式工作。复选框返回的结果非常疯狂......

这是我正在使用的代码: 对于形式:

<input type="checkbox" value="Box1" name="BoxGroup[]" />Box1
<input type="checkbox" value="Box2" name="BoxGroup[]" />Box2
<input type="checkbox" value="Box3" name="BoxGroup[]" />Box3

对于动作脚本:

if($_POST['BoxGroup'] == true){ // If one of the checkboxes were checked...
    foreach($_POST['BoxGroup'] as $value){
    $BoxGroup .= ", ".$value; // Make the array into a string
    }
    $BoxGroup = substr($BoxGroup,2); // To skip ", " from the beginning of the $BoxGroup variable
}

现在,该脚本的作用是:当用户发送表单时,它会检查是否选中了其中一​​个复选框,如果是,它将生成一个字符串,如下所示:“值,值”等。 我将这些值插入到我的数据库中。当我在页面上预览提交到数据库的内容时,我得到“射线/值/值”,——因此只有“射线”(如“数组”中)被传递给第一个框。

不幸的是,我无法更新服务器的 PHP 版本,因为系统操作员和我都没有 root 密码(我知道这很疯狂)。

那我该怎么办?

I made a script before, that worked completely fine in PHP 5.2. But when recently going over to my friends server (version PHP 4.4.9), I noticed some actions didn't work the way they should. The outcome of what the checkboxes returned came out crazy...

This is the code I'm using:
For the form:

<input type="checkbox" value="Box1" name="BoxGroup[]" />Box1
<input type="checkbox" value="Box2" name="BoxGroup[]" />Box2
<input type="checkbox" value="Box3" name="BoxGroup[]" />Box3

For the action script:

if($_POST['BoxGroup'] == true){ // If one of the checkboxes were checked...
    foreach($_POST['BoxGroup'] as $value){
    $BoxGroup .= ", ".$value; // Make the array into a string
    }
    $BoxGroup = substr($BoxGroup,2); // To skip ", " from the beginning of the $BoxGroup variable
}

Now, what this script does, is; when a user sends the form, it checks if one of the checkboxes were checked, and if so, it will make a string, like so: "value, value" etc.
I insert these values to my database. When I preview what's been submitted to the database on a page, I get "ray / value / value", -- so only "ray" (as in "Array") was passed for the first box it seems.

Unfortunately, I can not update the server's version of PHP, since both the system operator and I, don't have the root password to it (I know it's crazy).

So what do I do?

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

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

发布评论

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

评论(3

漫漫岁月 2024-10-12 02:35:10
implode(', ', array_keys($_POST['BoxGroup']))
implode(', ', array_keys($_POST['BoxGroup']))
无风消散 2024-10-12 02:35:10

我推荐 implode 函数:
http://php.net/manual/en/function.implode.php

因此,您的整个操作将变得更短并且更容易概览。
干杯

I'd recommend the implode function:
http://php.net/manual/en/function.implode.php

So your whole operation will getting a bit shorter and easier to overview.
cheers

孤独患者 2024-10-12 02:35:10

关于 implode 的其他评论是很好的建议,但我不知道它如何解决您的问题。 (嗯,如果您将变量初始化为 implode 的返回值,实际上可以,但这并不能解决这里的核心问题。)

正确初始化 $BoxGroup。我敢打赌 $BoxGroup = 'Array'; 在循环运行之前...可能是由于 register_globals 打开了。哎呀,禁用它。

在 .htaccess 中:

php_flag register_globals off

为了详细说明,我假设如果您执行了 var_dump($BoxGroup),您会发现由于 register_globals 它已经包含了这些值。当您将数组与另一个字符串连接时,该数组将被视为字符串“Array”。

The other comments regarding implode are good advice, but I don't see how it fixes your problem. (Well, it actually could, if you initialize the variable to the return value of implode, but that doesn't fix the core issue here.)

Initialize $BoxGroup properly. I bet $BoxGroup = 'Array'; before the loop even runs... probably due to register_globals turned on. Eeeek, disable that.

In .htaccess:

php_flag register_globals off

To elaborate, I assume that if you did a var_dump($BoxGroup), you'd see that it already contains those values thanks to register_globals. The array gets treated as a string 'Array' when you concat it with another string.

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