提交表单后无法读取复选框数组的值
我不知道我在这里错过了什么,但我尝试制作一个非常简单的表单,其中有一个复选框,这里是 html:
(...)
<input type="checkbox" name="test[]" value="js"> Javascript <br>
<input type="checkbox" name="test[]" value="php"> PHP <br>
<input type="checkbox" name="test[]" value="sql"> SQL <br>
<input type="checkbox" name="test[]" value="html"> HTML <br>
(...)
这是应该处理它的 php 片段:
echo '<pre>';
print_r($_POST['test']);
echo '</pre>';
print_r($_POST)
echo '<hr>';
这是输出
Array
Array
(
[stuff1] => 0
[stuff2] => 5
[stuff3] => 2
[test] => Array
)
的另一个输入表单显示得很好,但我无法解析数组的内容,这只是一个名为“array”的示例字符串......
如果我尝试做一个
var_dump($_POST["test"]); //this is what I get: string 'Array' (length=5)
I don't know what I miss here but I try to make a really simple form with a checbox in it, here is the html:
(...)
<input type="checkbox" name="test[]" value="js"> Javascript <br>
<input type="checkbox" name="test[]" value="php"> PHP <br>
<input type="checkbox" name="test[]" value="sql"> SQL <br>
<input type="checkbox" name="test[]" value="html"> HTML <br>
(...)
and here is the php snippet which suppose to handle it:
echo '<pre>';
print_r($_POST['test']);
echo '</pre>';
print_r($_POST)
echo '<hr>';
here is the output
Array
Array
(
[stuff1] => 0
[stuff2] => 5
[stuff3] => 2
[test] => Array
)
The other input of the forms are well display but I can't parse the content of the array which is just a sample string named "array"...
If i try to do a
var_dump($_POST["test"]); //this is what I get: string 'Array' (length=5)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个关于如何使用 php 处理复选框的很好的教程: http://www.html-form-guide.com/php-form/php-form-checkbox.html
基本上你的
$_POST['test']
是一个数组,如果没有选中复选框,则为空。如果用户选择js
和php
,则结果类似于["js","php"]
。如果您想循环浏览所有选定的选项,您可以这样做:
There is a good tutorial about how to handle checkbox using php : http://www.html-form-guide.com/php-form/php-form-checkbox.html
Basically your
$_POST['test']
is an array, that is empty if no checkbox was checked. And that would be like["js","php"]
if the user selectedjs
andphp
.If you want to cycle through all selected choices, you could do :
我找到了它,原因是我正在使用一个旧框架,该框架在激活或不激活 magic_quotes 的情况下都会发出奇怪的语句,我激活了 magic_quotes,因此它转义了 $_POST 变量中包含的任何数组...
我只是删除了此代码片段并一切顺利
I found it, the reason is that I'm using an old framework that make a strange statement within or without magic_quotes activated, Ive got magic_quotes activated so it's escaping any array contained in the $_POST variables...
I just remove this snippet and everything went allright