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

Dimitris Tsioumas
Dimitris Tsioumas
1,649 Points

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.

Dimitris Tsioumas
Dimitris Tsioumas
1,649 Points
    <?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!

Dimitris Tsioumas
Dimitris Tsioumas
1,649 Points
    <?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 ?

Dimitris Tsioumas
Dimitris Tsioumas
1,649 Points

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");
Dimitris Tsioumas
Dimitris Tsioumas
1,649 Points

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"?

Dimitris Tsioumas
Dimitris Tsioumas
1,649 Points

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! ;)