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 Simple PHP Application Adding a Contact Form Working with Get Variables

Get Variable and Redirect issue

I've followed this tutorial carefully, but when I moved over my contact-process and contact-thank-you pages all within my contact.php file, i'm not getting the redirect to the thank you page. My code is as follows:

<?php

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

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;

// TODO: Send Email

header("Location: contact.php?status=thanks");
exit;

}

?><?php

$pageTitle = 'Contact Mike'; $section = 'contact';
include('inc/header.php');

?>

<div class="section page">

<div class="wrapper">

    <h1>Contact</h1> 

    <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") { ?>
        <p>Thanks for the email! I&rsquo;ll be in touch shortly.</p>
    <?php  } else { ?>

        <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>

        <form method="post" action="contact.php">

            <table>
                <tr>
                    <th>
                        <label for="name">Name</label>
                    </th>
                    <td>
                        <input id="name" type="text" name="name">
                    </td>
                </tr>
                <tr>
                    <th>
                        <label for="email">Email</label>
                    </th>
                    <td>
                        <input id="email" type="text" name="email">
                    </td>
                </tr>
                <tr>
                    <th>
                        <label for="message">Message</label>
                    </th>
                    <td>
                        <textarea id="message" type="text" name="message"></textarea>
                    </td>
                </tr>
            </table>

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

        </form>

    <?php } ?>

</div>

</div>

<?php include('inc/footer.php'); ?>

Also - why does the video ask me to close my <?php statement after the condition in my if statement (before the paragraph element) and then reopen a new php statement before the closing } , then open the else { and, again, close the php statement before the rest of the html? Why couldn't all of this be in a single php statement?

Thank you for your help!!

1 Answer

Jose Soto
Jose Soto
23,407 Points

Your server request method should be checking against "POST" (uppercase), not "post" (lowercase). It should look like this:

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

That should resolve the issue you're seeing.