0

I am just starting out learning PHP and i am getting an error in my code. I am currently watching a tutorial and i wrote it the exact same but it doesn't work.

The error message is

Notice: Undefined index: grade in C:\xampp\htdocs\victor\switchStatement\index.php on line 14

Here is my code:

<html>
    <head>
        <title></title>
    </head>

    <body>
        <form action="index.php" method="post">
            What was your grade?
            <input type="text" name="grade">
            <input type="submit">
        </form>
            <?php
                $grade = $_POST["grade"];
                switch($grade)
                {
                    case "A":
                        echo "Wow that was really cool!";
                    break;

                    case "B":
                        echo "Wow that was... pretty cool.";
                    break;  

                    case "C":
                        echo "Wow that wasn't that cool.";
                    break;

                    case "F" || "D":
                        echo "Wow that was really bad!";
                    break;
                }
            ?>
    </body>
</html> 
Roman
  • 2,530
  • 2
  • 27
  • 50
Victor
  • 7
  • 2
  • 3
    `if(isset($_POST["grade"])).....` – B001ᛦ Aug 13 '20 at 11:40
  • 2
    On the first call of your page `$_POST` will not be set and `$grade = $_POST["grade"];` will throw that notice. Either check if `$_POST["grade"];` is set or only run that code if `$_SERVER['REQUEST_METHOD'] == 'POST'` – brombeer Aug 13 '20 at 11:41

0 Answers0