3

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
  • hi, how are you able to convert checkbox which i think is usually inside a form into a GET format? Commented Jul 18, 2012 at 7:00

4 Answers 4

10

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 :-)

Sign up to request clarification or add additional context in comments.

3 Comments

Now, can you tel me howto make it into variable? Example : I have foo checkbox array and I want the value is being included into one variable. Thanks
sorry, i really don't understand your question :( you mean implode to merge the array into one string?
If you see %5B and %5D in the URL it's just Unicode for [ and ]
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">

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.