user3491008 user3491008 - 1 month ago 6
PHP Question

Values from checkbox group are correct in querystring, but are not arrays in the $_GET variable

I have a form, with a checkbox group which looks like this:

<label><input type="checkbox" name="countries" value="US" class="chkcountries" checked="checked" /> United States</label><br />
<label><input type="checkbox" name="countries" value="FR" class="chkcountries" checked="checked" /> France</label><br />
<label><input type="checkbox" name="countries" value="CA" class="chkcountries" checked="checked" /> Canada</label><br />
<label><input type="checkbox" name="countries" value="AU" class="chkcountries" checked="checked" /> Australia</label><br />
<label><input type="checkbox" name="countries" value="UK" class="chkcountries" checked="checked" /> United Kingdom</label><br />
<label><input type="checkbox" name="countries" value="DE" class="chkcountries" checked="checked" /> Germany</label><br />
<label><input type="checkbox" name="countries" value="Ja" class="chkcountries" checked="checked" /> Japan</label><br />
<label><input type="checkbox" name="countries" value="IT" class="chkcountries" checked="checked" /> Italy</label><br />
<label><input type="checkbox" name="countries" value="IN" class="chkcountries" checked="checked" /> India</label><br />
<label><input type="checkbox" name="countries" value="ES" class="chkcountries" checked="checked" /> Spain</label><br />
<label><input type="checkbox" name="countries" value="BR" class="chkcountries" checked="checked" /> Brazil</label><br />
<label><input type="checkbox" name="countries" value="NL" class="chkcountries" checked="checked" /> Netherlands</label><br />
<label><input type="checkbox" name="countries" value="MX" class="chkcountries" checked="checked" /> Mexico</label><br />


When I select more than one country and run the form and print_r both the query string and the $_GET array, the querystring looks like this:


report=rawdata&sales=sales&kenp=kenp&startdate=&enddate=&format=table&track=unit&countries=US&countries=CA&countries=AU&countries=UK&allchannels=All&channels=2&channels=1&books=5&submit=Get+Report


and the $_GET array looks like this:


Array ( [report] => rawdata [sales] => sales [kenp] => kenp [startdate] => [enddate] => [format] => table [track] => unit [countries] => UK [allchannels] => All [channels] => 1 [books] => 5 [submit] => Get Report [SQLiteManager_currentLangue] => 2 )


As you can see, it's only selecting the last item in the countries array. (The same thing happens in checkbox groups on this form, e.g. channels).

I'm working in a test environment running MAMP on my Mac, FWIW.

What's weird is that it was working properly. (I've tried rebooting MAMP and then rebooting my entire system to see if that makes a difference, and it doesn't seem to.)

It's possible I've missed something dumb somewhere, but I've now been looking at it so long, I'm going cross-eyed. Any assistance would be greatly appreciated.

Answer

PHP determines if form data should be presented as a string or an array based on the name of the field, not the number of times it appears.

Append [] to the name attribute in the HTML.