PHP (mutiple select) and checkbox Problem
Scenario:
You are coding a HTML form with the following form elements:
- checkbox
- mutiple select
Normally you would name the [checkbox] a “name” for all instances example:
<input type="checkbox" name="btype" value="a"> Type A <input type="checkbox" name="btype" value="b"> Type B <input type="checkbox" name="btype" value="o"> Type O <input type="checkbox" name="btype" value="ab"> Type AB
For your [mutiple select] you just add “mutiple” on the select tag to make the dropdown element select mutiple items defined on the options tag.
Problem:
When submitting a group of checkboxes with the same name and a mutiple select, values passed on POST is the last item selected. For example, if you selected all checkbox named [btype] you are expecting the values (a,b,o,ab) will be returned as a string. But the problem is on PHP only value (ab) is returned.
In PHP POST and GET variables are treated differently, in order to retrieve all selected checkboxes and select option, you need to name your form elements with a “[]” (bracket) after the name, implying that the element will contain array values. In PHP you can define form elements as array values by adding “[]”, you can also explicitly define key names for array like for example:
Name: <input type="text" name="personal[name]"> Email: <input type="text" name="personal[email]">
By doing that you can access that values on PHP by:
foreach($personal as $key => $value) {
print "$key => $value<br>";
}
One more problem that you might encounter is when using javascript to handle checkboxes and mutiple select, one classic example is for checkbox if you want to add a “Check All” button which selects all checkboxes, remember that you had named the checkboxes “btype[]” in order to define that variable as array, thus retrieving all selected checkboxes on PHP end. But how in the world can you access that name on javascript. Since javascript is also using “[]” to define array the best possible way to access form elements named with “[]” brackets are using:
... document.forms[0].elements['btype[]']; ...
Summary:
When working on multiple select and checkboxes in PHP name this elements with “[]” to instruct PHP to store the values on array example:
<input type="checkbox" name="btype[]" value="a"> Type A <input type="checkbox" name="btype[]" value="b"> Type B <input type="checkbox" name="btype[]" value="o"> Type O <input type="checkbox" name="btype[]" value="ab"> Type AB <select name="season[]" multiple> <option value="spring">Spring</option> <option value="summer">Summer</option> <option value="autumn">Autumn</option> <option value="spring">Winter</option> </select>
You can access it on PHP as regular arrays, note that in PHP 3, the array form variable usage is limited to single-dimensional arrays. As of PHP 4, no such restriction applies. Accessing named form elements with “[]” on javascript using this code:
... document.forms[0].elements['btype[]']; ...
Bookmark This!














I am Filipino Web Developer, focusing on PHP in LAMP framework. As a kid, I spent a lot of my time exploring computers and computer games from Atari to PS, from INTEL 80286 - CoreDuo. I am happily married, with two kids. Currently working in Japan as an IT Engineer.