PHP/Form/Form Checkbox

Материал из Web эксперт
Перейти к: навигация, поиск

A form with checkboxes using the same name to store multiple values

   <source lang="html4strict">

<html> <head>

   <title>Using Default Checkbox Values</title>

</head> <body> <?php $food = $_GET["food"]; if (!empty($food)){

   echo "The foods selected are: ";
   foreach($food as $foodstuff){
       echo "
".htmlentities($foodstuff); } echo "
.";

} else {

   echo ("
   <form action="". htmlentities($_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!" />
   </form> ");
   }

?> </body> </html>

 </source>
   
  


Checking input from a checkbox or a multiple select

   <source lang="html4strict">

<?php $options = array("option 1", "option 2", "option 3"); $valid = true; if (is_array($_GET["input"])) {

   $valid = true;
   foreach($_GET["input"] as $input) {
       if (!in_array($input, $options)) {
           $valid = false;
       }
   }
   if ($valid) {
       //process input
   }

} ?>

 </source>
   
  


Responding to Checkboxes

   <source lang="html4strict">

<html> <head> <title>Checkbox Demo</title> </head> <body>

Checkbox Demo

Demonstrates checkboxes

<form action ="HandleFormCheckBox.php">

  • <input type ="checkbox" name ="chkFries" value ="11.00">Fries
  • <input type ="checkbox" name ="chkSoda" value ="12.85">Soda
  • <input type ="checkbox" name ="chkShake" value ="1.30">Shake
  • <input type ="checkbox" name ="chkKetchup" value =".05">Ketchup

<input type ="submit"> </form> </body> </html>


      </source>
   
  


Using Default Checkbox Values

   <source lang="html4strict">

<html> <head>

   <title>Using Default Checkbox Values</title>

</head> <body> <?php $food = $_GET[food]; $self = htmlentities($_SERVER["PHP_SELF"]); if (!empty($food)) {

   echo "The foods selected are:
"; foreach($food as $foodstuf) { echo "".htmlentities($foodstuf)."
"; }

} else {

   echo ("<form action=\"$self\" ");
   echo ("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>

 </source>
   
  


Validating a single checkbox

   <source lang="html4strict">

<?php $value = "yes"; echo "<input type="checkbox" name="subscribe" value="yes"/> Subscribe?"; if (isset($_POST["subscribe"])) {

   if ($_POST["subscribe"] == $value) {
       $subscribed = true;
   } else {
       $subscribed = false;
       print "Invalid checkbox value submitted.";
   }

} else {

   $subscribed = false;

} if ($subscribed) {

   print "You are subscribed.";

} else {

   print "You are not subscribed";

}

 </source>