INPUT Checkbox Type
- The checkbox can represent only two possibilities:
- When checked, it passes the value on to the $_POST array, but otherwise, it just doesn’t send anything.
Single check box
<form action="cb2.php" method="post">
Do you need money?
<input type="checkbox" name="a1" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
In the PHP script (cb2.php), we can get the submitted option from the $_POST array.
If $_POST['a1'] is “Yes”, then the box was checked.
If the check box was not checked, $_POST['formWheelchair'] won't be set.
Example:- PHP handling the form:
<?php
if(isset($_POST['a1']) &&
$_POST['a1'] == 'Yes')
{
echo "Need money.";
}
else
{
echo "Do not Need money.";
}
?>
The value of $_POST['formSubmit'] is set to ‘Yes’ since the value attribute in the input tag is ‘Yes’.
One Form, Multiple Processing
For one form, multiple processing use a check box group.
<form action="cb4.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>
The checkboxes have the exact same name ( formDoor[ ] ).
- Using the same name indicates that these checkboxes are all related.
- Using [ ] indicates that the selected values will be accessed by the PHP script as an array.
- That is, $_POST['formDoor'] won't return a single string.
- it will instead return an array consisting of all the values of the checkboxes that were checked.
- 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 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.
==============================================
0 Comments