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

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.