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

Jamaal Todd
Jamaal Todd
5,689 Points

PHP header not redirecting

Okay so, ive tried reading all the different discussions to try help me, but I cant seem to find anything (maybe its there, and im just not picking it up).

Everything woks fine up until I add in the header(Location: "contact-thanks.php").

Thanks in advance! :)

Jamaal Todd
Jamaal Todd
5,689 Points

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."Mesage: ".$message;


// TODO: Send Email 

header("Location: contact-thanks.php");
?>
Jamaal Todd
Jamaal Todd
5,689 Points

contact-thanks.php

<?php

$pageTitle = "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"); ?>

4 Answers

Hugo Leon
Hugo Leon
7,763 Points

Make sure you have set the name of your file correctly. 404 Error shows up when the page can't be found.

Jamaal Todd
Jamaal Todd
5,689 Points

Thanks Mike!

I found it just before I read your comment, and your comment backed it up!. I had my file saved as

"contact.-thanks.php" instead of "contact-thanks.php"

Little typo had me running around, thanks for the help!

Hugo Leon
Hugo Leon
7,763 Points

Hello Jamaal!

The function header needs a String as a required parameter. You must pass the string to the function as follows:

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

Hope it helps!

Jamaal Todd
Jamaal Todd
5,689 Points

Hi Hugo! :)

Thanks for the fast reply.

I've posted my code up above. I realise I did have it as you specified, can you plesae check over my code and see if theres anything wrong?

When I click submit, it goes to a page that says "object no found... etc... "

Thanks

Hugo Leon
Hugo Leon
7,763 Points

I do not see any problems in the code. But maybe you could try to show errors that may be generating your application. You could add the following lines at the beginning of your PHP file temporarily.

ini_set('display_errors', 1);
error_reporting(E_ALL);
Jamaal Todd
Jamaal Todd
5,689 Points

I added in the line of code like you said? im wondering if my position is right?. Im not sure what its doing , but I cant see it making any difference.

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$pageTitle = "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"); ?>

p.s thanks for the hlep

Hugo Leon
Hugo Leon
7,763 Points

I see, maybe the semicolon after including the header:

include("inc/header.php");
Jamaal Todd
Jamaal Todd
5,689 Points

I have corrected that now, but problem still happens. Object not found.... Error 404... etc

include("inc/header.php");