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

lindseyfowler
lindseyfowler
4,526 Points

Redirecting

Hi,

I'm attempting to create a contact form on a HTML page. I've been using this tutorial to help me along and have come to a stop at this step. When I use header(Location: ...) the contact-process page doesn't load after 'Send' is clicked.

Any and all help is appreciated =]

Can we see your code?

3 Answers

Guillermina Frind

header does not work if there is any output to the page before it. In this case you have included header.php before the header redirect. Since header.php does echo output to the page it would cause the redirect not to work.

lindseyfowler

Not sure if this helps with your problem but without seeing your code I can only suggest that you make sure you don't have any output to the page before header(Location...

Hi, I was following the tutorial, and can't seem to make it work either. I can't find any mistakes (I am a beginner though) and I can't find a difference with the code that's in the tutorial.

Here is the contact-process.php

<?php

include('includes/header.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");

?>

Here's the contact-thanks.php

<?php

$pageTitle = "Contact Mike"; $section = "contact"; include('includes/header.php');

?>

<div class="section page">

    <div class="wrapper">

        <h1>Contact us</h1>

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

    </div>

</div>

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

When I click send, so it submits the data to $_POST, it redirects to contact-process.php and shows it blank.

Can anybody spot the mistake?

Thank you!

Thanks Jason Anello! That was very helpful

You're welcome.