Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Milton Centeno
PLUS
Milton Centeno
Courses Plus Student 7,940 Points

contact.php not displaying "Thanks for the email!" message

Greetings, I am working on the "Working with Get Variables" PHP module and have been following along but my code is not working as it is in the video. My form should redirect to the same page but show the thank you message. Instead it simply shows the blank form again. Below is my contact.php code. Thanks.

<?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");

} ?>

<?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"]) AND $_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 type="text" name="name" id="name">
                        </td>
                    </tr>
                        <tr>
                        <th>
                            <label for="name">Email</label> 
                        </th>
                        <td>
                            <input type="text" name="email" id="email">
                        </td>
                    </tr>
                        <tr>
                        <th>
                            <label for="name">Message</label> 
                        </th>
                        <td>
                            <textarea name="message" id="message"></textarea>
                        </td>
                    </tr>
                </table>
                <input type="submit" value="Send">
            </form>
        <?php } ?>
    </div>
</div>

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

3 Answers

Check your server error logs. By default php/apache won't allow you to send headers (such as your header(location:…)) after the page headers have already been sent. So, you'll either need to change your server config file to allow that or you'll have to add <?php ob_start(); ?> at the very start of your page and then <?php ob_end_flush(); ?> before your <body> tag.

Milton Centeno
PLUS
Milton Centeno
Courses Plus Student 7,940 Points

I checked the log files and no indication of errors related to headers. I did an internet search for header redirect and get tons of links to info not really addressing this issue. Do you have a tutorial link or something that might help resolve this. The instructor in the videos did not make any mention this being an issue as we follow along with his code.

Try modifying this line of code

<?php if (isset($_GET["status"]) AND $_GET["status"] == "Thanks") { ?>

To this:

<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>

Try seeing if that is why you are getting a blank page.