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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s