Using PHP checkbox array with GET Method

懵懂的女人 提交于 2019-12-21 20:28:38

问题


Can I use PHP checkbox array with the GET Method? And if there are two checkboxes clicked, will that result in: some.php?param=1&param=2 ?

As you know, I am not used to write into database, I'm just getting the parameters. Thanks.


回答1:


You need to pass the checkboxes with the same field name followed by [] so PHP recognizes the array.

Like so:

<input type="checkbox" name="foo[]" value="bar1">
<input type="checkbox" name="foo[]" value="bar2">
<input type="checkbox" name="foo[]" value="bar3">

So the GET would be:

phpfile.php?foo[]=bar1&foo[]=bar2&foo[]=bar3

if every checkbox is clicked. (POST similar)

Please note, that only the clicked checkboxes will be submitted. So, if just bar1 and bar2 are clicked, then the GET would be

phpfile.php?foo[]=bar1&foo[]=bar2

Then you can access that array via

$_GET["foo"]

or similar to POST

$_POST["foo"]

Hope that helps :-)




回答2:


In addition to what steve said, you could also add certain keys if you'd like to:

<input type="checkbox" name="foo[my_id_1]" value="bar1">
<input type="checkbox" name="foo[my_id_2]" value="bar2">
<input type="checkbox" name="foo[my_id_3]" value="bar3">



回答3:


Let be the checkbox names are aa and values are 1 and 2 respectively .Put the form method as GET and the checkbox names are aa[0],aa[1] . when you submit the form, the data will pass in url like some.php?aa[0]=1&aa[1]=2




回答4:


some.php?param[]=1&param[key]=2


来源:https://stackoverflow.com/questions/11535743/using-php-checkbox-array-with-get-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!