Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Basic PHP Website (2018) Adding a Basic Form Checking the Request Method

Modure Rares
PLUS
Modure Rares
Courses Plus Student 9,041 Points

Why i get the notice Undefined index status????

I hace this php code but it triggesr an notice and the ?status is not working. When i ckick the submit button it should only show the thanking text, instead it shows file not found error please help me

if($_SERVER["REQUEST_METHOD"] == "POST") {

        $name = $_POST["name"];
        $email = $_POST["email"];
        $details = $_POST["suggestion"];

        echo "<pre>";
        $email_body = "";
        $email_body .= "Name: " . $name . "\n";
        $email_body .= "Email: ". $email . "\n";
        $email_body .= "Details: " . $details ."\n"; #this represents a linebreak 
        echo $email_body;
        echo "</pre>";

        // EMAIL SENDING CODE HERE 

        //**********************************************

        header("location:suggest.php$stage=thanks");
    }

    $pageTitle = "Suggest a Improvement";
    $section = "suggest";

    include("include/header.php");
 ?>


    <form id="suggestion" method="post" action="suggest.php">

        <h1 id="suggest-title">Suggest an Improvement</h1>

        <?php if($_GET["stage"] == "thanks" && isset($_GET["stage"])) {

                echo "<div id='thanks'><h1> Thank You! </h1></div>";
            } else {?>
        <fieldset>

            <label for="name">Name:</label>
            <input id="name" type="text" name="name">

            <label for="email">Email:</label>
            <input type="email" name="email" id="email">

        </fieldset>

        <fieldset>

            <label for="suggestion">Suggest:</label>
            <textarea name="suggestion" id="user_ideea">your idea...</textarea>

        </fieldset>

        <fieldset>

            <input type="submit" value="Send">

        </fieldset>
    </form>
    <?php } ?>
 </body>
 </html>

2 Answers

It looks like to me the problem here is this line:

header("location:suggest.php$stage=thanks");

In this case, there are 2 problems: $ is in place of ? and "stage" is in place of "status".

So think of the header("location:"); function like you would an url, when a get query is made it uses "?{variable}=" instead of "${variable} =" like in PHP.

So what you'd like here is to have "?status=" as the variable the file is looking for is status, and ? denotes the start of variables.

To go further, in an url, you want to separate variables with an & symbol (although since there is only one variable being passed along in this case, the status variable, we don't need to worry about that).

Madhuri Charugundla
Madhuri Charugundla
2,383 Points

header("location:suggest.php$stage=thanks") -- Problem is with this line. Use ? instead $.