php 多个复选框共享相同的名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16552680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Several Checkboxes sharing the same name
提问by Buzu
According to the w3c "Several checkboxes in a form may share the same control name. Thus, for example, checkboxes allow users to select several values for the same property." However, if you do that, PHP will only take the last value. For example:
根据 w3c “表单中的多个复选框可能共享相同的控件名称。因此,例如,复选框允许用户为同一属性选择多个值。” 但是,如果您这样做,PHP 将只采用最后一个值。例如:
<?php
if ($_POST) {
echo "<pre>";
print_R($_POST);
echo "</pre>";
}
?>
<form action="" method = "post">
<input type="checkbox" name="pet" value="dog" />Dog<br />
<input type="checkbox" name="pet" value="Cat" />Cat<br />
<input type="checkbox" name="pet" value="bird" />bird<br />
<input type="checkbox" name="pet" value="iguana" />iguana<br />
<input type="submit" />
</form>
If you submit that form, you will see that only the checked box that appears last will be set. The browser sends them all, but they overwrite each other. So, setting the same name to several checkboxes can cause problems. Has it always been like that? I seem to remember that it was possible to actually send all the values as an array.
如果您提交该表单,您将看到只设置最后出现的复选框。浏览器将它们全部发送,但它们会相互覆盖。因此,为多个复选框设置相同的名称可能会导致问题。一直都是这样吗?我似乎记得实际上可以将所有值作为数组发送。
I know that you can just add an [] at the end of the name to create an array of values:
我知道你可以在名称的末尾添加一个 [] 来创建一个值数组:
<?php
if ($_POST) {
echo "<pre>";
print_R($_POST);
echo "</pre>";
}
?>
<form action="" method = "post">
<input type="checkbox" name="pet[]" value="dog" />Dog<br />
<input type="checkbox" name="pet[]" value="Cat" />Cat<br />
<input type="checkbox" name="pet[]" value="bird" />bird<br />
<input type="checkbox" name="pet[]" value="iguana" />iguana<br />
<input type="submit" />
</form>
But the w3c doesn't specify that. Honestly I don't remember if I always used the [] at the end of the name, but for some reason I think at some point I didn't. Was there any time in the past when you could make it work without the []?
但是 w3c 没有具体说明。老实说,我不记得我是否总是在名称末尾使用 [],但出于某种原因,我想在某些时候我没有。过去有没有什么时候你可以在没有 [] 的情况下让它工作?
采纳答案by sourcejedi
That would never have worked without the []
, not in PHP.
如果没有 , 这将永远无法工作,而[]
不是在 PHP 中。
W3C don't specify anything about how query strings are handled server-side. (Ignoring an irrelevant, obsolete corner of the CGI spec, only relevant to PHP in that it was a security hole up until recently).
W3C 没有指定关于如何在服务器端处理查询字符串的任何内容。(忽略CGI 规范中一个不相关的、过时的角落,仅与 PHP 相关,因为它直到最近还是一个安全漏洞)。
It looks like that pattern is valid markup, but not commonly used, for the reason you describe.
由于您描述的原因,该模式似乎是有效的标记,但并不常用。
A similar pattern isused for radio buttons, of which only one can be selected at a time. (In fact, giving the radio inputs the same name is how the browser knows to treat them as a group). Perhaps that's what you were thinking of.
一个类似的模式被用于无线电按钮,其中只有一个可在一个时间被选择。(事实上,给无线电输入相同的名称是浏览器知道将它们视为一个组的方式)。也许这就是你的想法。
回答by Vedran ?ego
If you really want it in PHP, try this:
如果你真的想在 PHP 中使用它,试试这个:
<?php
if (count($_POST)) {
header("Content-type: text/plain");
$fp = fopen("php://input", "r");
fpassthru($fp);
fclose($fp);
exit;
}
?>
<form action="" method = "post">
<input type="checkbox" name="pet" value="dog" />Dog<br />
<input type="checkbox" name="pet" value="Cat" />Cat<br />
<input type="checkbox" name="pet" value="bird" />bird<br />
<input type="checkbox" name="pet" value="iguana" />iguana<br />
<input type="submit" />
</form>
More on php://input
stream can be found in PHP documentation.
更多关于php://input
流的信息可以在PHP 文档中找到。