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.
Mike Zhu1,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:
<?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"); ?>
<?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 FlaniganCourses Plus Student 15,803 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!
Try to remove the header and call it just after the opening php tag in your contact-process.php
Mike Zhu1,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 Fernandez8,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;