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 Checking the Request Method

For some reason I am unable to get the header command to work. Every time it just loads the form submission in theheader

Here is my code:

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

        // TODO; send email

    header("Location: contact-thanks.php");
    exit;
    }

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

    <div class="section page">
        <div class="wrapper">
            <h1>Contact</h1>
            <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>

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

3 Answers

You should never use the header function when output above it is echo'd. It simply won't work.

So, when you remove this from your code:

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

You will be redirected to the contact-thanks.php page.

Hugo Paz
Hugo Paz
15,622 Points

Jeffrey is correct, check the php manual http://php.net/manual/en/function.header.php

Thank you.