Synook

Checkboxes and passing multiple values through arrays

When creating a form, one way to get users to select a conbination of elements out of several is to use a series of checkboxes. On the server end, however, without going to the length of naming each checkbox individually then running through all of them (which could get irritating when there are lots of checkboxes or they are generated dynamically), how do you know which ones have been selected?

The answer is to use one name for all the checkboxes in each group, but to put at the end of the names the two characters [] – which indicate that they belong to an array. Then, when you recieve the form, the checked values will simply have populated an array with the same name as the name you gave the checkboxes.

Option 1: <input type="checkbox" name="check[]" value="option1" />
Option 2: <input type="checkbox" name="check[]" value="option2" />
Option 3: <input type="checkbox" name="check[]" value="option3" />
Option 4: <input type="checkbox" name="check[]" value="option4" />