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 Build a Simple PHP Application Adding a Contact Form Redirecting After a Form Submission

My PHP header refuses to redirect. Help!

<?php

$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:coctact-thanks.php');

?>

No echo statements beforehand.. I have looked in the contact page and the index page as well as the header and footers. I'm at a complete loss here.

3 Answers

Hi Adam,

You have a typo in your file name "coctact-thanks.php"

I actually progressed a little further and found that the redirect is replaced and it works fine now. I did fix that typo and still couldn't get it to open 'contact-thanks.php' though.

so weird.

The conditional is a much more streamlined solution anyways I suppose.

Yes, later on you end up doing everything within "contact.php"

I wonder if you had a blank line or any whitespace before your opening php script. That also prevents the redirect from happening.

If I remember correctly, the instructor used a conditional statement to redirect the header. Checking if it had a method of post or not (or get). I would rewatch the video.

Also it could be because of "coctact" when it should say contact. header("Location: contact-thanks.php");

Hi Ricardo,

I believe he's at an earlier version before that.

Wow, I scoured so many pages removing echo statements in the includes and really just wracking my brain. Of course it was a typo. Thanks so much for the quick reply gentleman.