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 Redirecting After a Form Submission

Mike Zhu
Mike Zhu
1,840 Points

PHP Header Redirecting Not Working

My code is like that: after clicking submit, the file still remains at contact-process.php, instead of the new "contact-thanks.php" file.

This is the contact-process.php file

<?php
// TODO: Send Email
$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;
header("Location: contact-thanks.php");
?>

This is the contact-thanks.php file:

<?php

$pageTitle = "Contact Mike";
$section = "Contact";
include("header.php");

?>

<div class = "section page">
    <div class = "wrapper">
        <h1>Contact</h1>
        <p>Thanks for the Email. We will contact you soon.</p>
    </div>
</div>

<?php
include("footer.php");
?>
Hugo Paz
Hugo Paz
15,622 Points

Hi Mike,

header() must be called before any output is sent to the browser. One of the most common causes of this problem is adding an extra line after the closing PHP tag in an include file.

Check if you have any whitespace after the closing tag in the contact-process.php file.

1 Answer

Devon Scott
Devon Scott
16,396 Points

Actually, If that is the entire contents of your contact-process.php file, you can play it safe by removing the closing PHP tag ( ?> ) to ensure that absolutely nothing is being output to mess with your header() function.