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++">
C++<br>
<input name="language[]" type="checkbox" value="Java">
Java<br>
<input name="language[]" type="checkbox" value="PHP">
PHP<br>
<input name="language[]" type="checkbox" value="ASP">
ASP<br>
<input name="language[]" type="checkbox" value="Delphi">
Delphi<br>
<input name="send" type="submit" id="send" value="Send!">
</form>

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.

<?php
if(isset($_POST['language']))
{
$language = $_POST['language'];
$n = count($language);
$i = 0;

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

Taken From : http://www.php-mysql-tutorial.com/wikis/php-tutorial/php-forms.aspx

1 comment: