使用$ _GET将多个具有相同名称的html复选框添加到URL [英] Adding multiple html checkboxes with the same name to URL using $_GET

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

问题描述

我有一个网站搜索表格.搜索表单包括一个类别部分,用户可以在其中选择不同类别的项目,所有这些都是复选框.它们都具有相同的名称,因此我可以使用一些javascript全选,而全都不选.我需要使用php $ _GET以便可以向url添加参数,以便可以返回到用户最初搜索的条件.

I have a search form for my website. The search form includes a category section where the user can select items from different categories, all of which are checkboxes. They all have the same name so that I can use some javascript to select all and select none. I need to use php $_GET so that I can add arguments to the url so that i can get back to the criteria that the user originally searched.

html:

<input name="catID[]" id="catID_10" type="checkbox" value="10" />
<input name="catID[]" id="catID_11" type="checkbox" value="11" />
<input name="catID[]" id="catID_12" type="checkbox" value="12" />
...

提交from之后的网址,实际的括号被转换为html字符串形式,其格式类似于"catID%5B%5D = 10& catID%5B%5D = 11& catID%5B%5D = 12"

The url after the from has been submitted, the actual brackets are transformed into html string form and is formatted like "catID%5B%5D=10&catID%5B%5D=11&catID%5B%5D=12"

所以我想知道,一旦提交表单以更改每个复选框的名称,以便可以在url中正确格式化但不会弄乱其他全选/全选功能,我可以执行一些JavaScript吗? /p>

So im wondering, is there some javascript i can perform, once the form is submitted to change the name of each checkbox so that it will format right in the url but not mess up the other select all/select none functionality?

推荐答案

对于URL中的php样式的数组,这是完全正常的.您会发现,如果执行print_r($_GET),将会得到类似的内容:

That's perfectly normal for a php-style array in a URL. You'll find that if you do print_r($_GET), you'll get something like:

array(
   'catID' => array(
       0 => 10,
       1 => 11,
       2 => 12
   )
   'otherparameter' => 'value'
)

,并且可以通过简单的方法获得各个复选框的值

and can get at the individual checkbox values with a simple

if (is_array($_GET['catID'])) {
     foreach($_GET['catID'] as $catID) {
        ...
     }
}

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

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