Handling a Checkbox With PHP
To make your website simpler and to get the best information from the online visitors you need to provide them with options like Yes or No which make it easier for them to browse. On the website you can do that if you can provide them with HTML checkboxes. These checkboxes are usually provided when you want to ask them a query like do they accept the terms and conditions that you have provided on the website or they don't like and want to discontinue. You can provide a checkbox option where online visitors can select their option and proceed further. In HTML, a simple checkbox looks something like this. However, since we are dealing with PHP here we will need to know how to handle the checkbox option in PHP. If you are going for a very simple checkbox option where you don't have much options you can try it this way in HTML.
<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>
However, when you turn to PHP the code will look somewhat like this.
<?php
if(isset($_POST['formWheelchair']) &&
$_POST['formWheelchair'] == 'Yes')
{
echo "Need wheelchair access.";
}
else
{
echo "Do not Need wheelchair access.";
}
?>
In this PHP checkbox code the value of POST is set to Yes, but you can change that to whatever you want to on the website.
If you are looking for a checkbox in group which normally happens when you are providing your online visitors with some kind of form to fill up then you need to follow this code.
<?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] . " ");
}
}
?>
Posted by Michael on 2010-10-14 in the category " php "
