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 Validating Form Data

Alex Flores
Alex Flores
7,864 Points

Help understanding PHP form GET & POST.

I'm working on "Building a PHP Website" and I'm just trying to understand what is happening when I submit a form. Here is the code trimmed to include just the key concepts:

    if($_SERVER["REQUEST_METHOD"] == "POST") 
    {
    ob_start(); //Cannot alter header on page once output has started or just put 
    echo "<pre>";
    $email_body = "";
    $email_body .= "First Name " . $firstName . "\n";
    $email_body .= "Last Name " . $lastName . "\n";
    $email_body .= "Email " . $email . "\n";
    echo $email_body;
    echo "Hello $firstName";
    echo "</pre>";

    header("location: register.php?status=thanks"); 
    }
    ?>
 <div class="registration">
    <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") {
        echo "<p>Thanks for registering!</p>";
    } else { ?>
           <form method="post" action="register.php"> 
           </form>
      <?php } ?>
 </div>

So the action of form is set to post/submit to register.php. So then the script re-runs and this time since there's a post, the top half of the code runs. The header() then redirects it to "register.php?status=thanks". What is this exactly? I would think it would lead it to a dead page since at the moment this script runs there is no "register.php?status=thanks", but it actually runs registration.php.

I was just hoping someone could explain this to me better or point in the direction that would explain it better.

2 Answers

Hi there Alex!

Yes, you are correct. One small amendment.

It re-runs the register.php

When you submit form, page by default will be reloaded(not re-runned) and script inside will rerun ;).

Best regards.

Alex Flores
Alex Flores
7,864 Points

Thanks for your help!

Hi there Alex!

When you are typing in your address bar

http://example.com/register.php?param1=value&param2=value

you are making a GET request with GET params(param1=value&param2=value).

or simple request without parameters.

http://example.com/register.php

In this case if statement will evaluate to false. Because request type is GET, not POST. In POST method data were transferred in the body of the request(not in the query string like in GET method).

But, nevertheless when you submit form, your data will be transferred via POST method this time. And after page reloads and ob_start(); gather all output into buffer from echo statements only then you will be redirected to another page with info about user and "Thanks for registering!" message.

Hint

If you want to see how it works, open your browser console and go to the Network tab. In which you can see what type of request you are making.

Hope this will explain few things.

Best regards.

Alex Flores
Alex Flores
7,864 Points

That's very helpful. Thank you. Just to make sure I get this, when I submit the form as a POST type it's submitted to the register.php page. It re-runs the register.php page and since it's a POST method the first IF statement is passed and ob_start() puts all of the output into the buffer and redirects to register.php with the search query "status=thanks". This search query is a GET request (because it's GETting the redirection location).

Then the second IF passes because the appropriate parameters are met. Is that right?