PHP/Form/Radio Button — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 07:04, 26 мая 2010
Checking input from a radio button or a single 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) {
}
}
?>
Form radio buttons
<html>
<head>
<title>Your Favorites</title>
</head>
<body>
<form action="fav.php" method="post">
<b>Please enter your first name:</b>
<input type="text" size="45" name="username"> <br>
<b>Please select your favorite color wine:</b> <br>
<input type="radio" name="color" value="white"> White
<input type="radio" name="color" value="rosé"> Rosé
<input type="radio" name="color" value="red"> Red <br>
<b>Please enter your favorite dish:</b>
<input type="text" size="45" name="dish"><br><br>
<input type="submit" value="Submit This Form">
</form>
</body>
</html>
File: fav.php
<html>
<head>
<title>Your submission</title>
</head>
<body>
<br>
<?php
$username = $_POST["username"];
$color = $_POST["color"];
$dish = $_POST["dish"];
if( $username != null )
{
echo( "Thanks for your selection $username <hr>" );
}
if( ( $color != null ) && ( $dish != null ) )
{
$msg = "You really enjoy $dish <br>";
$msg .= "- especially with a nice $color wine";
echo( $msg );
}
?>
</body>
</html>
Validating a radio button
<?php
$choices = array("eggs" => "E",
"toast" => "T",
"coffee" => "C");
foreach ($choices as $key => $choice) {
echo "<input type="radio" name="food" value="$key"/> $choice \n";
}
if (! array_key_exists($_POST["food"], $choices)) {
echo "You must select a valid choice.";
}
?>