Home >>PHP Programs >Get Multiple checkbox value in PHP

Get Multiple checkbox value in PHP

How to get multiple checkbox value in PHP

Check box is a small box on a form into which a tick or other mark is entered as the response to a question. When we need more than one choice for single question then we use check box. As we already know that when we fetch the value of any of the field (text field, dropdown, checkbox etc) of the form then we need to have the name of the field i.e. we fetch the value of the field by it’s name and as we are fetching more than one value through check box so we need to have the name of that field an array(use to store multiple values in a single name), so that we can store all value fetched from check boxes in the single name. Here is the example to fetch the values from multiple checkbox:

PHP Script

<?php
 
extract($_POST);

if(isset($save))

{

if(!empty($chklist))
	
{
	
echo "<h3>You have selected these items :</h3>";
	
echo "<ol type='A'>";
	
foreach($chklist as $val)
	
{
	
echo "<li>";
	
echo $val;
	
echo "</li>";
	
}
	
echo "</ol>";
	
}
	
else
	
{
	
echo "<font color='red'>pls select your items</font>";
	
}

}

?>

HTML Form to define multiple check-box

<form method="post">

<table border="1">

<tr>

<td>Chocolate</td>

			
<td><input type="checkbox" value="Chocolate" name="chklist[]"/></td>
		
</tr>
		
<tr>

			
<td>Coffee</td>
			
<td><input type="checkbox" value="Coffee" name="chklist[]"/></td>
		
</tr>
		
<tr>
			
<td>Tea</td>
			
<td><input type="checkbox" value="Tea" name="chklist[]"/></td>
		
</tr>
		
<tr>
			
<td>Buiscuit</td>
			
<td><input type="checkbox" value="Buiscuit" name="chklist[]"/></td>
		
</tr>
		
<tr>
			
<td align="center" colspan="2"><input type="submit" value="Show my Items" name="save"/></td>
		
</tr>
	
</table>

</form>

No Sidebar ads