问题
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¶m=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¶m[key]=2
来源:https://stackoverflow.com/questions/11535743/using-php-checkbox-array-with-get-method