html form guide
  about

Handling checkbox in a PHP form processor

This tutorial will introduct HTML check boxes and how to deal with them in PHP.

Single check box

Let's create a simple form with a single check box that indicates whether wheelchair access is needed or not.

<form action="checkbox-form.php" method="post">
<p>
  Do you need wheelchair access?
  <input type="checkbox" name="formWheelchair"
value="Yes" />
</p>
<input type="submit" name="formSubmit" value="Submit" />
</form>

In PHP, use the standard $_POST array to access the value, just as before. If $_POST['formWheelchair'] is "Yes", then the box was checked. If it's "", then the box was not checked. Here's an example of PHP handling the form:

<?php
  if($_POST['formSubmit'] != "") {
    $varWheelchair = $_POST['formWheelchair'];
   
    if($varWheelchair == "") {
      echo("<p>You do NOT need wheelchair access.</p>\n");
    } else {
      echo("<p>You DO need wheelchair access.</p>\n");
    }
    exit();
  }
?>

That's all there is to it. You could make a whole group of checkboxes using this method, but there is a much easier way to do it with PHP.

Check box group

There are often situations where a group of related checkboxes are needed on a form. Let's build on the above example and give the user a list of buildings that he is requesting door access to.

<form action="checkbox-form.php" method="post">
<p>
  Which buildings do you want access to?<br />
  <input type="checkbox" name="formDoor[]"
value="A" />Acorn Building<br />
  <input type="checkbox" name="formDoor[]"
value="B" />Brown Hall<br />
  <input type="checkbox" name="formDoor[]"
value="C" />Carnegie Complex<br />
  <input type="checkbox" name="formDoor[]"
value="D" />Drake Commons<br />
  <input type="checkbox" name="formDoor[]"
value="E" />Elliot House
</p>
<p>
  Do you need wheelchair access?
  <input type="checkbox" name="formWheelchair"
value="Yes" />
</p>
<input type="submit" name="formSubmit" value="Submit" />
</form>

Notice that all the building checkboxes have the exact same name. Also notice that each name ends in []. This is done on purpose. Using the same name indicates that these checkboxes are all related. Using [] indicates that the selected values will be accessed by PHP as an array. That is, $_POST['formDoor'] won't return a single string like it's been doing up until now; it will instead return an array consisting of all the values of the checkboxes that were checked. For instance, if I checked all the boxes, $_POST['formDoor'] would be an array consisting of: {A,B,C,D,E}. Here's an example of how to retrieve the array of values and display them:

<?php
if($_POST['formSubmit'] != "") {
  $varWheelchair = $_POST['formWheelchair'];
  $aDoor = $_POST['formDoor'];
 
  if($varWheelchair == "") {
    echo("<p>You do NOT need wheelchair access.</p>\n");
  } else {
    echo("<p>You DO need wheelchair access.</p>\n");
  }
 
  if(is_null($aDoor)) {
    echo("<p>You didn't select any buildings.</p>\n");
  } else {
    echo("<p>You selected door(s): ");
    for($i=0; $i < count($aDoor); $i++)
    {
      echo($aDoor[$i] . " ");
    }
    echo("</p>");
  }

    exit();
  }
?>

If no checkboxes are checked, $_POST['formDoor'] will be "null", so use the "is_null" function to check for this case. If it's not null, then this example just loops through the array (using the "count" function to determine the size of the array) and prints out the building codes for the buildings that were checked. Usually this type of loop will be used to construct multiple SQL queries to insert the data. The "implode" PHP function can also be used to combine each element of the array into one string.

Download the php form checkbox sample code.

  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Related pages

 
  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb
Email forms
  How to get email from html form submission

Email form using 'mailto'

Form to email using PHP

Email form with file attachment in PHP

Form mail scripts
  Form mail script selection guide

Perl based form mail

Contact Forms
  PHP based email contact form

Secure your HTML contact form using captcha
PHP Form
  PHP form validation script

PHP form tutorial: first steps

PHP form processing

PHP form 'GET'

PHP form 'POST'

Handling checkbox in PHP form processor

Handling select box in a PHP form

Using PHP_SELF in the action field of a form

How to submit a form using PHP

Creating a registration form using PHP

Making a login form using PHP

Creating a multi-page order form using PHP

Passing PHP form variables from one page to other

  Copyright © 2008 html-form-guide.com . All rights reserved.