Monday, October 12, 2009

PHP : Get Multiple Check box values

Using Array As Form Values

Take a look at the code example below. The form have five input with the same name, language[]. Using the same input name is common for checkboxes or radio buttons.

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Select the programming languages you can use<br>
<input name="language[]" type="checkbox" value="C++">
<input name="language[]" type="checkbox" value="Java">
<input name="language[]" type="checkbox" value="PHP">
<input name="language[]" type="checkbox" value="ASP">
<input name="language[]" type="checkbox" value="Delphi">
<input name="send" type="submit" id="send" value="Send!">

The PHP code below print the value of language after the form is submitted. Go ahead and try the example. Try checking and unchecking the options to see the effect.

$language = $_POST['language'];
$n = count($language);
$i = 0;

echo "The languages you selected are \r\n" .
while ($i < $n)
echo "<li>{$language[$i]}</li> \r\n";
echo "</ol>";

Taken From :

1 comment: