View Full Version : Contact Form W Required Fields

12-08-2005, 02:15 PM
I Like To Add To My Contact Form

A Menu Tree Of Checkboxes

Option A
Option B

And Have Checkboxes To Be Required Fields

Please Help Me Improve My Contact Form. Thanks

12-11-2005, 09:29 PM
[...] Checkboxes

Option A
   3I'll assume you created your checkboxes along the lines of:

<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:

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:

foreach($_POST['option-a'] as $value) {
/* ... */
Hope that helps,