如何使用PHP获取组中复选框的值? [英] How to get the values of check boxes in a group with PHP?

查看:42
本文介绍了如何使用PHP获取组中复选框的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中添加了一组复选框,如下所示:

I have a form where I added a group of check boxes like below:

<form action="next.php" method="get">
<input name="c1" value="1" type="checkbox">
<input name="c1" value="2" type="checkbox">
<input name="c1" value="3" type="checkbox">
<input name="c1" value="4" type="checkbox">
...
</form>

如果选中第一个和第三个复选框,则按下提交按钮时,URL看起来不错,例如

If I select the first and the 3rd check box, the URL looks good when I press the submit button, like

?c1=1&c1=3 [...]

,但是$ _GET数组仅保留c1的最后一个值.我知道我可以使用 [] 表示法获取数组,但是链接看起来像

but the $_GET array holds only the last value of c1. I know I could use the [] notation to get an array, but then the link looks like

?c1[]=1&c1[]=3 [...]

我看到一些网站生成的URL没有 [] ,但仍然设法将所有值都考虑在内.他们是如何做到的?

I saw sites that generate the URL without the [] and still manage to take all values into account. How do they do that ?

谢谢!

推荐答案

如果要对多个不同的表单元素使用相同的字段名,PHP要求使用伪数组表示法:

PHP requires you to use a pseudo-array notation if you want to use the same fieldname for multiple different form elements:

<input type="checkbox" name="c1[]" ... />
<input type="checkbox" name="c1[]" ... />
<input type="checkbox" name="c1[]" ... />
etc...

[] 告诉PHP将 c1 视为一个数组,并保留所有提交的值.如果没有 [] ,PHP会简单地用新的值覆盖每个先前的c1值,直到完成表单处理为止.

The [] tells PHP to treat c1 as an array, and keep all of the submitted values. Without [], PHP will simply overwrite each previous c1 value with the new one, until the form's done processing.

不使用 [] 表示法的站点自己进行表单处理,从 $ _ SERVER ['QUERY_STRING'] 中检索查询字符串,然后自行解析

Sites which don't use the [] notation do the form processing themselves, retrieving the query string from $_SERVER['QUERY_STRING'] and then parse it themselves.

这篇关于如何使用PHP获取组中复选框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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