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

Jeff Hoffman
Jeff Hoffman
6,196 Points

Problem Redirecting a PHP Form

I am following along with the PHP introduction track, and I am stuck because I can't get the page to redirect from contact-process.php to contact-thanks.php. Is there a way someone can look at my pages and tell me what the problem is? I don't know how I share the URL or files. All I have is this: http://localhost/

Aaron Graham
Aaron Graham
18,033 Points

The best thing for you to do would be to post your code directly to the forum. You can use the Markdown Cheatsheet (link right below answer box) to see how to best format your code.

Hi Jeff,

You can't share your local files.

You can paste your code in here and somebody can take a look at it. See this thread for how to do it: https://teamtreehouse.com/forum/posting-code-to-the-forum

Post your code for "contact-process.php" if you can.

Jeff Hoffman
Jeff Hoffman
6,196 Points

The first section of code is my "contact-process.php". The second section is my "contact-thanks.php". Any ideas of what might be wrong would be really helpful.

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

?>
<?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 will be in touch shortly.</p>

        </div>

    </div>

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

2 Answers

Aaron Graham
Aaron Graham
18,033 Points

When you say you can't get it to redirect, do you mean that nothing happens at all, or does it try to redirect but just gives you an error? If you are getting an error, i.e. page not found, check the file path of contact-thanks.php. With the redirect location you are using, this file should be in the document root of your server. If that isn't where it is, either change the redirect to match the file path as it relates to the document root (something like header("Location: /actual/path/from/document/root/contact-thanks.php")), or move the file to the document root.

If you aren't getting a not found error, check to make sure that there aren't any HTML or php echo statements before the redirect. If what you have above for contact-process.php is all that is in the file, you are probably OK, however if contact-process.php is being included by another file that does have HTML or echo statements, it might be an issue. My guess is that you are getting to contact-process.php from a form submission (i.e. from some form element like this: <form method="post" action="contact-process.php">). If that is correct, this probably isn't your issue.

Last but not least, how are you serving these documents? Are other php files rendering OK? If not, you might have an issue with php not being enabled.

Given you set of circumstances, do any of these sound possible?

Jeff Hoffman
Jeff Hoffman
6,196 Points

I dug a little further back into my code and found that I accidentally left "contact-process.php" as "inc/contact-process.php" which I had later moved out of that folder. All fixed. Thanks for the help.

Finding these bugs is definitely the toughest thing about coding - a small enough error with a big enough consequence could drive a person crazy.