Monday 3 September 2018

Using the PHP checkbox array with the GET method

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.

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"]

0 comments:

Post a Comment