Handling checkbox in a PHP form processor

This tutorial will introduce 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.

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


In the PHP script (checkbox-form.php), we can get the submitted option from the $_POST array. If 
$_POST['formWheelchair'] is "Yes", then the box was checked. If the check box was not checked, $_POST['formWheelchair'] won't be set.

Here's an example of PHP handling the form:

<?php

if(isset($_POST['formWheelchair']) &&
   $_POST['formWheelchair'] == 'Yes')
{
    echo "Need wheelchair access.";
}
else
{
    echo "Do not Need wheelchair access.";
}   

?>

The value of $_POST['formSubmit'] is set to 'Yes' since the value attribute in the input tag is 'Yes'.

<input type="checkbox" name="formWheelchair" value="Yes" />

You can set the value to be a '1' or 'on' instead of 'Yes'. Make sure the check in the PHP code is also updated accordingly.

Check box group

There are often situations where a group of related checkboxes are needed on a form. The advantage of check box group is that the user can select more than one options. (unlike a radio group where only one option could be selected from a group).

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

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

<input type="submit" name="formSubmit" value="Submit" />

</form>

Please note that the checkboxes have the exact same name ( formDoor[ ] ). Also notice that each name ends in [ ]. Using the same name indicates that these checkboxes are all related. Using [ ] indicates that the selected values will be accessed by PHP script as an array. That is, $_POST['formDoor'] won't return a single string as in the example above; 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
  $aDoor $_POST['formDoor'];
  if(empty($aDoor))
  {
    echo("You didn't select any buildings.");
  }
  else
  {
    $N count($aDoor);

    echo("You selected $N door(s): ");
    for($i=0; $i $N$i++)
    {
      echo($aDoor[$i] . " ");
    }
  }
?>

If no checkboxes are checked, $_POST['formDoor'] will not be set, so use the "empty" function to check for this case. If it's not empty, 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.

If the check box against 'Acorn Building' is checked, then the array will contain value 'A'. similarly, if 'Carnegie Complex' is selected, the array will contain C.

Download Sample Code

Download the PHP form checkbox sample code:
php-form-checkbox.zip.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值