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

Mike Zhu
Mike Zhu
1,840 Points

PHP Redirecting using header(); NOT WORKING

After clicking the "submit" button, the page goes to the "contact-process.php" page which stores the data, instead of redirecting to the new created page "contact-thanks.php" page, using header(); commands.

My codes are as follows:

contact-process.php:

<?php
// TODO: Send Email
$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;
header("Location: contact-thanks.php");
?>

contact-thanks.php:

<?php

$pageTitle = "Contact Mike";
$section = "Contact";
include("header.php");

?>

<div class = "section page">
    <div class = "wrapper">
        <h1>Contact</h1>
        <p>Thanks for the Email. We will contact you soon.</p>
    </div>
</div>

<?php
include("footer.php");
?>
Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

Reformatted your post a bit to make the code display correctly. The backticks to open code blocks seem to work better with an empty line above them.

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

Mike,

Can you post your code for contact.php? I think that may hold the key.

4 Answers

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,815 Points

Not sure if you're having the same issue, but there was a similar question a few months back (click here to read the thread), and the solution had to do with blank space at the top of the content-process.php file. If anything is output to the browser before the header function is called, it won't work.

Hope this helps!

Mike Zhu
Mike Zhu
1,840 Points

The code for contact.php is:

?php 

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

?>

    <div class = "section page">
        <div class = "wrapper">
            <h1>Contact</h1>
            <p>I&rsquo;d love to hear from you!</p>
            <form method = "post" action = "conteact-process.php">
                <table>
                    <tr>
                        <th>
                            <label for = "name">Name</label>
                        </th>
                        <td>
                            <input type = "text" name = "name" id = "name">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <label for = "email">Email</label>
                        </th>
                        <td>
                            <input type = "text" name = "email" id = "email">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <label for = "message">Massage</label>
                        </th>
                        <td>
                            <textarea name = "message" id = "message"></textarea>
                        </td>
                    </tr>
                </table>
                <input type = "submit" value = "Send">
            </form>

        </div>
    </div>

<?php include('footer.php'); ?>
Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

Looks like you've misspelled "contact-process.php" in your form's action attribute. Try correcting that and see if it does the trick.

Mike Zhu
Mike Zhu
1,840 Points

Thanks so much! I have corrected the misspelling problem but it seems does not work either. The page goes to "contact-process.php" instead of "contact-thanks.php", which is very troublesome.

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,815 Points

Another thing I just noticed on your contact.php page...on your first line, there is no < to open your php block. This shouldn't be causing the problem, but it's worth fixing.

As for the issue at hand...I think we're back to the contact-process.php file, and I keep coming back to my above answer. Did you double-check for any extra spaces at the top of the file? All it takes is a single blank space before your opening <?php to throw off the header function.

Try to remove the header and call it just after the opening php tag in your contact-process.php

Mike Zhu
Mike Zhu
1,840 Points

Thank you so much for all your answers! I have solved the problems!!!

I checked the code, and THE FIRST LINE in my code actually is a BLANK LINE, and the code actually starts from the second line, and that is where the problem is!!! After I delete and put all the code from the the first line, it works!

I am not sure why this can happen. But after delete the blank line, it works!

Jonathan Fernandez
Jonathan Fernandez
8,325 Points

I want to note that I was going crazy with this problem for a good 10 min.. If anyone is here and likes to leave left over code snippets for note. Comment them out or move them somewhere else. Been looking at the spaces when I really should of done something about echo $email_body;