Contact Form - Checkboxes
         by kirupa : 20 December 2004

In the previous page, we wrapped up our simple form. In this and the next page, I will cover adding new form elements such as option buttons, checkboxes, and drop-down menus.

Implementing a checkbox in HTML is easy! Getting them to work in PHP is a little less straightforward. Let's look at our basic HTML example again, this time, with checkboxes added:

<form method="POST" action="mailer2.php">
<input type="text" name="name" size="19"><br>
<input type="text" name="email" size="19"><br>
<input type="checkbox" name="check[]" value="blue_color"> Blue<br>
<input type="checkbox" name="check[]" value="green_color"> Green<br>
<input type="checkbox" name="check[]" value="orange_color"> Orange<br>
<textarea rows="9" name="message" cols="30"></textarea><br>
<input type="submit" value="Submit" name="submit">

Since our HTML code is very similar to what you had earlier, I have only emphasized the checkbox code. Here is our modified mailer.php code that accommodates the use of checkboxes:

if(isset($_POST['submit'])) {
$to = "[email protected]";
$subject = "Form Tutorial";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg";
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
} else {
echo "blarg!";

Checkbox Explanation
Note the code in pink our modified HTML code. The value of each textbox that gets passed to the PHP file is a brief description of what the textbox is.

The code in red is the actual name of each checkbox. Notice that, unlike the two text fields earlier, I do not give each checkbox a different name. The value for the checkbox name is an array variable called check[].

Basically, here is how it works. For each checkbox you check, the check[] array receives the value of the checkboxes pressed. For example, after checking both the green and blue checkboxes, your check[] array contains the values green_color and blue_color as its elements.

In other words, each checkbox value ends up being an element of the check[] array. Now, if you remember, we have to get all of our form elements stashed into one variable ($body) that can be sent as an argument for the body into PHP's mail function.

So, what we need to do, is figure out a way of taking the values from the array and store it in a variable. That new variable can then be concatenated (linked together) with your previous form element data into our $body variable:

$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg";

As you can see from the above code, our checkbox values are stored nicely into the $check_msg variable. So, to reiterate, we require a way of taking the values from the check array and storing them in this variable. Well, PHP provides a great function that iterates through an array: foreach()

Here is our code using foreach():

foreach($_POST['check'] as $value) {
$check_msg .= "Checked: $value\n";

The array we are looping through is check, and we are storing each element of the array into a variable called $value. The body of the function simply increments the $check_msg variable with the value of each element in your array. For, foreach, simply goes through each element in your array, executes the body, goes to the next element in the array, executes the body, and so on until it reaches the end of the array.

This is similar to a do/while loop with the end test being when you reach the end of your array. Most of the complexities of implementing such a loop, though, are hidden by the foreach function!

In the next page, I will provide the code for both option buttons and drop-down menus along with a brief description. The reason I am not going to go into detail about them is that they are simple variations of what I have detailed in this and the previous pages.

Onwards to the next page!

page 3 of 4


SUPPORTERS:'s fast and reliable hosting provided by Media Temple.