PHP/Form/Radio Button
Checking input from a radio button or a single 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) { }
} ?>
</source>
Form radio buttons
<source lang="html4strict">
<html>
<head> <title>Your Favorites</title> </head> <body> <form action="fav.php" method="post"> Please enter your first name: <input type="text" size="45" name="username">
Please select your favorite color wine:
<input type="radio" name="color" value="white"> White <input type="radio" name="color" value="rosé"> Rosé <input type="radio" name="color" value="red"> Red
Please enter your favorite dish: <input type="text" size="45" name="dish">
<input type="submit" value="Submit This Form"> </form> </body>
</html>
File: fav.php
<html>
<head> <title>Your submission</title> </head> <body>echo( "Thanks for your selection $username
<?php $username = $_POST["username"]; $color = $_POST["color"]; $dish = $_POST["dish"]; if( $username != null ) {
" );
} if( ( $color != null ) && ( $dish != null ) ) { $msg = "You really enjoy $dish
"; $msg .= "- especially with a nice $color wine"; echo( $msg ); } ?> </body>
</html>
</source>
Validating a radio button
<source lang="html4strict">
<?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.";
} ?>
</source>