stytown
stytown

Reputation: 1734

Switch statement in PHP

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

Upvotes: 3

Views: 6406

Answers (3)

Lawrence Cherone
Lawrence Cherone

Reputation: 46610

The reason you may be getting an error is your using $_POST["city"] before it was set by posting

I personally would not use a _POST _GET _REQUEST globals 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:

<?php 
if(isset($_POST['city'])){
    $city=$_POST['city'];
}else{
    $city='';
}
//Or simpler
$city=(isset($_POST['city']))?$_POST['city']:'';



//The use $city
switch($city){
    case "sf":
        echo "Welcome to San Francisco, enjoy the beautiful weather.";
        break;
    case "tokyo":
        echo "Welcome to Tokyo, enjoy the sushi.";
        break;
    ...
    ...

?>

Upvotes: 2

amindfv
amindfv

Reputation: 8446

The issue is that the switch statement tries to access $_POST["city"] in cases where there is none set. This is a perfect use for the isset() function:

if (isset($_POST["city")) {
   /* switch statement */
} else {
   /* form element*/
}

Upvotes: 3

Scott
Scott

Reputation: 21882

You need to add a break; after the default: option. And you've got a misplaced semi-colon after "paris" it should be a colon.

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

}

?>

Upvotes: 3

Related Questions