Learning PHP and MySQL

Example 10-05 A form with checkboxes using the same name to store multiple values

<html>
<
head>
<
title>Using  Default  Checkbox  Values</title>
</
head>
<
body>
<?
php
$food  
=  $_GET["food"];
if  (!empty(
$food)){
echo  "The  foods  selected  are:  <strong>";
foreach(
$food  as  $foodstuff){
echo  '<br  />'.$foodstuff;
}
echo  "</strong>.";
}
else  {
echo  ('
<form  action="'
.$_SERVER["PHP_SELF"].'"  method="GET">
<fieldset>
<label> Italian
<input  type="checkbox"  name="food[]"  value="Italian"  />
</label>
<label> Mexican
<input  type="checkbox"  name="food[]"  value="Mexican"  />
</label>
<label> Chinese
<input  type="checkbox"  name="food[]"  value="Chinese"  checked="checked"  />
</label>
</fieldset>
<input  type="submit"  value="Go!"  />'
);
}
?>
</body>
</html>

Posted by on 08/18 at 11:02 AM

Next entry: Figure 10-09 Selecting Italian and Chinese

Previous entry: Example 10-04 Multiple book types

<< Back to main