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,515
  • 5
  • 29
  • 54

3 Answers3

4

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
  • 898
  • 9
  • 19
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,271
  • 2
  • 22
  • 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,286
  • 5
  • 30
  • 43