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

header function not working

Hello,

I would like some help with the following problem:

No matter what I do, after clicking the "Send" button, I am not being redirected to "contact-thanks.php"

Post your php code so somebody can take a look.

    <?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: contact-thanks.php");

?>

Thanks!

    <?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: contact-thanks.php");

?>

Thanks!

5 Answers

Is that code at the top of your file? Do you have any blank lines before the opening <?php ?

yes it is,no I have no blank lines

Try putting the server url in. Like below without my brackets

header("Location: http://[INSERT SERVER URL HERE]/contact-thanks.php");

unfortunatelly the problem still persists, I have even tried ' ' instead of " " as found on W3School

Ok, let's verify a few things.

You do have contact-thanks.php created and in the same folder?

Is the action attribute on your contact form going to "contact-process.php"?

And is the code you posted above inside "contact-process.php"?

I quit everything, slept for a while, and now that I just tried it again it worked :P I would like to thank everybody for their help! ;)