Monthly Archives: November 2011

Get multiple checkbox values selected in PHP

<?php
$test_chk = array();

if(isset($_POST) && !empty($_POST)) {

	$test_chk = $_POST['test_chk'];

	if(empty($test_chk)){ 
		$test_chk = array();
	}
}
?>

<form method="post" action="">
	<strong>Multiple Check Test:</strong><br />
	
	<input type="checkbox" name="test_chk[]" value="value A" <?php if(in_array("value A", $test_chk)) echo ' checked="checked"';?> /> value A<br />
	<input type="checkbox" name="test_chk[]" value="value B" <?php if(in_array("value B", $test_chk)) echo ' checked="checked"';?> /> value B<br />
	<input type="checkbox" name="test_chk[]" value="value C" <?php if(in_array("value C", $test_chk)) echo ' checked="checked"';?> /> value C<br />
	<input type="checkbox" name="test_chk[]" value="value D" <?php if(in_array("value D", $test_chk)) echo ' checked="checked"';?> /> value D<br />
	
	<input type="submit" name="test_sbt" value="Submit" />
</form>
Advertisements