多个具有相同名称的复选框 [英] Several Checkboxes sharing the same name

查看:92
本文介绍了多个具有相同名称的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据w3c表单中的多个复选框可能共享相同的控件名称。因此,例如,复选框允许用户为同一属性选择多个值。但是,如果这样做,PHP将仅采用最后一个值。例如:

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:

<?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>

但是w3c没有指定。老实说,我不记得我是否总是在名字的末尾使用[],但是出于某种原因,我认为我并没有使用。过去有没有什么时间可以使它在没有[]的情况下工作?

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 []?

http://www.w3.org/TR/html401/interact/forms.html#checkbox

推荐答案

如果没有 [] 的话,那将永远无法实现,而不是PHP。

That would never have worked without the [], not in PHP.

W3C没有指定有关如何在服务器端处理查询字符串的任何内容。 (忽略无关紧要的 CGI规范的过时角落,仅与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).

看起来该模式是有效的标记,但由于您所描述的原因而并不常用。

It looks like that pattern is valid markup, but not commonly used, for the reason you describe.

类似的模式 用于单选按钮,一次只能选择一个。 (实际上,为广播输入提供相同的名称是浏览器知道如何将它们视为一个组)。也许这就是您的想法。

A similar pattern is used 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.

这篇关于多个具有相同名称的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆