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

Chase Lee
Chase Lee
29,275 Points

Redirecting After a Form Submission

Hi,

I am doing "Redirecting After a Form Submission" in "Build a Simple PHP Application", and got to the part where Randy has us take the users to the "contact-thanks.php". But it dosen't work for me.

Here is the code I have in contact-process.php:

<?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;
    echo $email_body;

    //TODO: Send Email

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

Here is contact-thanks.php:

    <?php 
    $page_title = "Contact Mike";
    $section = "contact";
    include("inc/header.php"); ?>

    <div class="section_page">

        <div class="wrapper">

            <h1>Contact</h1>

            <p>Thanks for the email. I&rsquo;ll be in touch shortly.</p>

        </div>

    </div>

    <?php include("inc/footer.php"); ?>

And here is a link to my layout.

Thanks.

2 Answers

Chase Lee
Chase Lee
29,275 Points

Figured it out. Had to take out the echo command.

Randy Hoyt
Randy Hoyt
Treehouse Guest Teacher

Yep. Once anything gets echo'd out or displayed to the screen, it is too late to set headers. Glad you got it working!

Hi I have a question about redirecting contact forms.

This question is for Randy,

If I'm placing my contact form in an iframe, or using an iframe, the function header () won't work, what do you suggest?

Thanks!