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

How does this php code work?

I am confused how the php code knows if the form is a post when the php code runs before the form is even made. Here is the code from the video.

'''php

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST"){ $name = $_POST["name"]; $email = $_POST["email"]; $message = $_POST["message"]; $emailBody = ""; $emailBody = $emailBody . "Name: " . $name . "\n"; $emailBody = $emailBody . "Email: " . $email . "\n"; $emailBody = $emailBody . "Message: " . $message;

// TODO: Send Email

//redirects to the thank you page 
header("Location: contact.php?status=thanks");
exit;

} ?>

'''

'''html

                <form method="post" action="contact.php">
                    <table>
                        <tr>    
                            <th><label for="name">Name</label></th>
                            <td><input type="text" name="name" id="name"></td>
                        </tr>

                        <tr>    
                            <th><label for="eamil">Email</label></th>
                            <td><input type="text" name="email" id="email"></td>
                        </tr>

                        <tr>    
                            <th><label for="message">Message</label></th>
                            <td><textarea name="message" id="message"></textarea></td>
                        </tr>

                    </table>
                    <input type="submit" value="Send">
                </form>

'''

1 Answer

Kevin Korte
Kevin Korte
28,148 Points

The php doesn't run initially, because the $_SERVER["REQUEST_METHOD" does not equal POST when the contact page is initially loaded.

That first if block if ($_SERVER["REQUEST_METHOD"] == "POST") causes the rest of the PHP mail code to not be executed.

Instead, it loads the form. When the form is submitted, it gets sent back to the same file via the action attribute in the form element. The same code is ran again, starting with the if block if ($_SERVER["REQUEST_METHOD"] == "POST"), which this time this comes out as true because of the method attribute of the form element. So now we run the PHP mail code, and finish it off with appending some code into the URL, and than exiting that code block.

Later down the road, you'll use PHP again to check the GET value to is if status is set, and if status equals thanks. If that evaluates true it'll display the message sent message you create, instead of the form.