PHP/Form/Form Checkbox

Материал из Web эксперт
Версия от 07:04, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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 />".htmlentities($foodstuff);
    }
    echo "</strong>.";
}
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>



Checking input from a checkbox or a multiple select

 
<?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
    }
}
?>



Responding to Checkboxes

<html>
<head>
<title>Checkbox Demo</title>
</head>
<body>
<h1>Checkbox Demo</h1>
<h3>Demonstrates checkboxes</h3>
<form action ="HandleFormCheckBox.php">
<ul>
  <li><input type ="checkbox" name ="chkFries" value ="11.00">Fries</li>
  <li><input type ="checkbox" name ="chkSoda"  value ="12.85">Soda</li>
  <li><input type ="checkbox" name ="chkShake" value ="1.30">Shake</li>
  <li><input type ="checkbox" name ="chkKetchup" value =".05">Ketchup</li>
</ul>
<input type ="submit">
</form>
</body>
</html>

<!-- HandleFormCheckBox.php
<html>
<head>
<title>Checkbox Demo</title>
</head>
<body>
<h3>Demonstrates reading checkboxes</h3>
<?
print <<<HERE
chkFries: $chkFries <br>
chkSoda: $chkSoda <br>
chkShake: $chkShake <br>
chkKetchup: $chkKetchup <br>
<hr>
HERE;
$total = 0;
if (!empty($chkFries)){
  print ("You chose Fries <br>");
  $total = $total + $chkFries;
}
if (!empty($chkSoda)){
  print ("You chose Soda <br>");
  $total = $total + $chkSoda;
}
if (!empty($chkShake)){
  print ("You chose Shake <br>");
  $total = $total + $chkShake;
}
if (!empty($chkKetchup)){
  print ("You chose Ketchup <br>");
  $total = $total + $chkKetchup;
}
print "The total cost is \$$total";
?>
</body>
</html>
-->



Using Default Checkbox Values

 
<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:<br />";
    foreach($food as $foodstuf)
    {
        echo "<strong>".htmlentities($foodstuf)."</strong><br />";
    }
}
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>



Validating a single checkbox

 
<?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";
}