IT & Programming

Working with checkboxes and PHP

  • How to process checkboxes with php.
  • How to get posted checkbox values with php.
  • Working with php and checkboxes.

This script process posted checkbox values sent from a form. We have 2 php pages. At Forms.php we have a form with checkboxes and a submit button. Once we click submit button after selecting few checkboxes, the page submits values to Save.php. Then we get all selected checkbox values with the help of for loop.

Form.php

<form action="Save.php" method="post">

	<input value="Belgium" name="country[]" type="checkbox" /> Belgium
	<input value="France" name="country[]" type="checkbox" /> France
	<input value="Germany" name="country[]" type="checkbox" /> Germany
	<input value="Holland" name="country[]" type="checkbox" /> Holland
	<input value="Greece" name="country[]" type="checkbox" /> Greece
	
	<input value="Submit" type="Submit" />

</form>

Save.php

if(!empty($_POST['country'])){

	for ($i=0; $i < count($_POST['country']);$i++) {
	
		echo $_POST['country'][$i] . ", ";
	} 
}

Output

Belgium, Germany, Greece,

Comments

  1. Viki April 04, 2014

    Nice one..best way....

  2. Kat Gee July 07, 2012

    Wonderfully simple example!! best i've seen!!

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *