Checkboxes and passing multiple values through arrays
By Synook in Tutorials, (X)HTML & CSS on Saturday August 23rd, 2008 | 1 comment | No diggs
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" />
1 comment
- That's a pretty cool tutorial. I would also like to add that it's very useful to
use the "FOR" condition as you could do this
$optionIDs = $_POST['optionID];
$countarray = count($optionIDs);
for($i=0; $i < $countarray; $i ++){
$optionID = $optionIDs[$i];
mysql_query("UPDATE *TABLENAME* SET *FIELD*= 'WHATEVER' WHERE id='$optionID'")
}
Rob
-----------------------------------
http://www.robslounge.com
Comment by Robert
RoundedDesign Blog
Categories
- (X)HTML & CSS (10)
- JavaScript (5)
- News (3)
- Other (3)
- PHP (8)
- The Lab (8)
- Tutorials (10)
