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

header("Location: contact-thanks.php"); does not work

header("Location: contact-thanks.php"); does not work. It gets a NULL value.

The conditional works.

Code is as follows, which is as it appears in the video.

I've found a common reason for this is that actual output is sent prior to the header(). I don't see where that would be happening here

<?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. "E-mail:".$email."\n"; $email_body=$email_body. "Message: ".$message."\n";

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

exit;  

}

2 Answers

I've had an issue like this when I had some white space after a closing PHP tag. Your code works fine for me:

<?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. "E-mail:".$email."\n"; 
    $email_body=$email_body. "Message: ".$message."\n";

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

This was close enough. The problem was a blank line at the top of the file. My opening <?php tag started on line 2. NoviceProblems.