3
<input type="checkbox" name="currency" value="usd"/>
<input type="checkbox" name="currency" value="euro"/>
<input type="checkbox" name="currency" value="cad"/>

Im trying to get currency values through $_GET request, something like /?currency=usd,cad but instead im getting /?currency=usd&currency=cad and then $_GET['currency'] returns only one value.

adding name=currency[] just gets /?currency[]=usd&currency[]=cad

What is the proper way to get these checkbox values in some sort of array?

skyisred
  • 6,855
  • 6
  • 37
  • 54

3 Answers3

5

HTML:

<input type="checkbox" name="currency[]" value="usd"/>
<input type="checkbox" name="currency[]" value="euro"/>
<input type="checkbox" name="currency[]" value="cad"/>

PHP:

<?php
foreach($_GET['currency'] as $currency){
  echo $currency."<br/>";
  //or what ever
}
?>
This_is_me
  • 908
  • 9
  • 20
0

Try this way: name="currency[]"

We have to tell the serverside that this is a name with multiple value.

$_GET['currency'] have to be an array this way.

Peter Kiss
  • 9,309
  • 2
  • 23
  • 38
0

In your html, make the checkbox name:

<input type="checkbox" name="currency[]" value="usd"/>

This will add the check values to an array. Your method, each check is overwriting the last in your $_GET

Brian Vanderbusch
  • 3,313
  • 5
  • 31
  • 43