By Martin / Tutorials, (X)HTML & CSS / Saturday August 23rd, 2008 / No comments
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" />
∎
I hope you enjoyed reading this article. If you wish, you may view some of the other recent or popular things I have written, or subscribe to receive RSS updates. You can also add a comment, or share this article on Twitter or Facebook, below.
Website copyright © Aspektas 2009 - 2010 Valid XHTML 1.0 Strict and CSS 2.1