I am trying to do some error checking. I want to make it so that if no radio button is checked on an html page, it will pass to the php and tell the user to choose a radio button and a link to go back to that page.
Included is my current code:
PHP
<?php
switch($_POST["city"]){
case "sf":
echo "Welcome to San Francisco, enjoy the beautiful weather.";
break;
case "tokyo":
echo "Welcome to Tokyo, enjoy the sushi.";
break;
case "paris";
echo "Welcome to Paris, enjoy the Eiffel Tower.";
break;
Default:
echo "Please pick a city.";
echo "<a href=\"/week7.html\">Go Back</a>";
}
?>
HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form action="Week7PHP.php" method="post">
<input type="radio" name="city" value="sf">San Francisco<br />
<input type="radio" name="city" value="paris">Paris<br/>
<input type="radio" name="city" value="tokyo">Tokyo<br/>
<input type="submit" id="Submit">
</form>
</div>
</body>
</html>
It is giving an error when it isn’t finding any of the radio buttons checked.
This is the error if no option is picked: Notice: Undefined index: city
The reason you may be getting an error is your using
$_POST["city"]before it was set by postingI personally would not use a
_POST_GET_REQUESTglobals within a switch.I would code a controller no matter how basic the script is that checks and handles these “special” variables.
Something as simple as: