
Originally Posted by
miradoro
[...] Checkboxes
Option A
   1
   2
   3
I'll assume you created your checkboxes along the lines of:
HTML Code:
<input name="option-a[]" type="checkbox" value="1">
<input name="option-a[]" type="checkbox" value="2">
<input name="option-a[]" type="checkbox" value="3">
The specifics aren't that important, however each group should share the same name, and that name should be followed a pair of square brackets.
When one or more of the checkboxes above are selected, an array will be created in the $_GET or $_POST superglobals (I should think you'll be using the POST transfer method) with the name of that checkbox group. Using the example above, the name would be 'option-a'.
If, for example, the second and third checkbox above was selected, the first element of the array (index 0) will have the value '2', and the second element (index 1) will have the value '3'. If the first and third checkboxes were selected, the first element will have the value '1', and the second element will have the value '3'.
If no checkboxes are selected, a browser won't send any form for them, so the array within $_POST won't exist.
So, to check if any data was submitted, we can use the isset function to test whether the array exists:
PHP Code:
if(isset($_POST['option-a'])) {
/* Element exists
*
* We can optionally check that the element is
* actually an array using the is_array function.
*/
}
You can now loop through the array with foreach, for instance, to obtain the values it contains:
PHP Code:
foreach($_POST['option-a'] as $value) {
/* ... */
}
Hope that helps,
Mike
Bookmarks