pTutorial

New way to learn Programming

PHP example

How to get value from check boxes

What is check box?

Like radio button, Check box is used to get one or more than one value while in case of radio button you can get only on value.

In this PHP example we will learn how to get multiple value from check boxes with the help of array. Here is the example to get multiple value from the form.

PHP Example

<html>
<body>
<form method="post">
<table border="2" cellpadding="5" cellpadding="5">
<tr>
<td>Mango</td>
<td><input type="checkbox" value="Mango" name="fruits[]"/></td>
</tr>
<tr>
<td>Orange</td>
<td><input type="checkbox" value="Orange" name="fruits[]"/></td>
</tr>
<tr>
<td>Banana</td>
<td><input type="checkbox" value="Banana" name="fruits[]"/></td>
</tr>

<td colspan="2"><input type="submit" value="Result" name="frm"/></td>
</tr>
</table>
</form>

<?php 

if(isset($_POST['frm']))
{
if(isset($_POST['fruits']))
{
foreach($_POST['fruits'] as $selected)
{
echo $selected."</br>";
}
}
else
{
echo "Select atleast one";
}
}

?>

</body>
</html>

Explanation

In the above PHP example, we used form with checkboxes and simple access with the help of array.

Note: The name of the input type of check box must be same and in the form of array.


QUOTES OF THE DAY

Crafty men condemn studies, simple men admire them, and wise use them.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER