PHP Forms and User Input (Cont.)


PHP Radio Buttons
Radio buttons are used when you want the user to select one of a limited number of choices.

HTML
radio.php
 <form method="post" action="radio.php">
  <input name="sex" type="radio"
    value="male"> Male
  <input name="sex" type="radio"
    value="female" checked> Female
  <input type="submit" name="act"
    value="Check"> 
 </form>
 <html><body>
 <?php
  echo  "Pick: $_POST[act]";
  echo  " & $_POST[sex]";
 ?>
 </body></html>
Male   Female      

PHP Checkboxes
A radio button is a way to restrict users to having only one choice. Like radio buttons, checkboxes are used to give visitors a choice of options. Whereas radio buttons restrict users to only one choice, you can select more than one option with checkboxes.

HTML
checkbox.php
 <form method="post"
   action="checkbox/checkbox.php">
  <input name="hs[]" type="checkbox"
    value="Biking"> Biking
  <input name="hs[]" type="checkbox"
    value="Hiking"> Hiking
  <input name="hs[]" type="checkbox"
    value="Reading"> Reading
  <input type="submit" name="act"
    value="Check"> 
 </form>
 <html><body>
 <?php
  echo "Pick: ";
  $hs = $_POST[hs];
  foreach ( $hs as $h )
   echo  "$h ";
 ?>
 </body></html>
Biking   Hiking   Reading