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

Bradley White
Bradley White
21,285 Points

Get Variable code not working.

Here is my code. It doesn't load the page. I've looked at it five ways to Sunday, found mistakes, fixed them, tried again, looked it again against the project files. There is something simple I'm not seeing. Please help.

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

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";

$email_body = $email_body . echo "Name: " . $name . "\n";
$email_body = $email_body . echo "Email: " . $email . "\n";
$email_body = $email_body . echo "Message: " . $message . "\n";
echo $email_body;

// 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"]) 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>
                    <th>
                        <label for="email">Email</label>
                    </th>
                    <td>
                        <input type="text" name="email" id="email">
                    </td>
                <tr>
                    <th>
                        <label for="message">Message</label>
                    </th>
                    <td>
                        <textarea type="text" name="message" id="message"></textarea>
                    </td>
            </table>

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

        <?php } ?>

        </div>
    </div>

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

3 Answers

Hi Bradley,

You're echoing your email body to the page before the header call.

header does not work if there is any output before it.

You have to make sure that there is no output including echo statements and even whitespace.

If you were to put a blank line in your php file right before your opening php tag then this could also cause header not to work.

Bradley White
Bradley White
21,285 Points

Thanks Jason,

I caught the email body echo after posting. The space above the first php tag is just on this post, its not in my actual code but Im glad this format error happened. I didn't know that blank line could cause an issue. I didn't catch the echo being deleted in the video when moving the code in from contact-process.php. I've gotten so use to echo that my mind seems to have ignored it when comparing between the files.

A code hour is wasted but my experience is basted! :-) (That's probably a bad joke.)

I was only speaking in general terms about the blank line. I didn't think you had it in your code. More along the lines of just something to watch out for in the future.

Glad you got it figured out.

Bradley White
Bradley White
21,285 Points

Yep thx again for your time!