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

The logic behind suggest.php file

As I understand, PHP runs the file from top to bottom like many other languages. Alena put the conditional:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST["name"];
    $email = $_POST["email"];
    $details = $_POST["details"];

    echo "<pre>";
    $email_body = "";
    $email_body .= "Name " . $name . "\n";
    $email_body .= "Email " . $email . "\n";
    $email_body .= "Details " . $details . "\n";
    echo $email_body;
    echo "</pre>";

    //To Do: Send Email
    header("location:suggest.php?status=thanks");
}

at the very top of the file. However, wouldn't this mean that PHP will skip over this conditional when the file is first run? The value of REQUEST_METHOD is only set after the form is sent, and the form is set after this conditional.

I guess what I'm asking is how does PHP know to check the conditional when the value of REQUEST_METHOD is set after the conditional itself?

1 Answer

Hi David,

By the end of this video, the suggest.php file is handling 3 different things.

You're correct that the first time you visit suggest.php the if condition will be skipped over and it will run the code below which loads up the form. The form is loaded instead of the thank you message because the url doesn't contain the query string "?status=thanks" at this point.

Once you fill out the form and submit it, it will submit back to the same file because of the action attribute. This means that php is now processing this file for the second time from the top down. And this time the if condition will be true because the form was posted. The email will be sent off and then a simple redirect back to the same page again but this time with the query string added.

That means that php will now process this file again for a third time from the top down. The if condition will be false this time and skipped over. The thank you message will get displayed instead of the form because "status=thanks" is in the query string now.

Let me know if that makes sense.

Thanks so much for your answer! That cleared up pretty much everything!

Hey, I just got another question. I have been practicing with forms, POST and GET on my own and I ran into some problems. I pretty much set up my practice file exactly the same as Alena in the video. However, in my file, any variables set before the redirect is not saved after the redirect and comes up as null. Why is this the case?

My code (please ignore the bad syntax :p) :

<?php 
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $test = $_POST["test"];
    header("location:test.php?status=done");
    exit;
  }  ?>
<html>
<head>
</head>
<body>
  <form action="test.php" method="POST">
      <?php if (isset($_GET["status"]) && $_GET["status"] == "done") {
                echo $test;
      } ?>
      <input type="text" name="test" id="test">
      <input type="submit">
  </form>
</body>
</html>