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 trialRon Kenzig
4,884 PointsForm Redirect not working
I'm trying to do a simple redirect from the contact-process.php to a contact-thanks.php page and it is still going straight to the contact-process.php? Am I missing something?
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$how = $_POST["how"];
$sport = $_POST["sport"];
$service = $_POST["service"];
$comments = $_POST["comments"];
$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Phone: " . $phone . "\n";
$email_body = $email_body . "How did you hear about us?: " . $how . "\n";
$email_body = $email_body . "What sport are you interested in?: " . $sport . "\n";
$email_body = $email_body . "Type of service?: " . $service . "\n";
$email_body = $email_body . "Comments: " . $comments;
echo $email_body;
// TODO: Send Email
header("Location: /contact-thanks.php");
?>
Here are the links to check it out for yourself:
http://192.196.158.148/~sesadmin/contact-2.php http://192.196.158.148/~sesadmin/contact-thanks.php
Any words of wisdom, Randy Hoyt?
13 Answers
Elliott Frazier
Courses Plus Student 9,647 PointsYou need to delete the echo command. Headers cannot be executed once information is outputted by using commands such as echo
,'var_dump',print_r
, ect..
After that it should be working fine! Hope this helps.
Ron Kenzig
4,884 PointsMy form method is set to "post" and the action is set to "contact-process.php" while the button below the form is set to "submit"
I believe all of those stay the same, but I just wanted to clarify.
When I click submit, it is still outputting the results of the form on the "contact-prcoess.php" page and is not redirecting to the "contact-thanks.php" page.
So confused.
Riley Hilliard
Courses Plus Student 17,771 PointsFrom looking at the lesson, it looks like you need to remove the slash at the beginning of the link:
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$how = $_POST["how"];
$sport = $_POST["sport"];
$service = $_POST["service"];
$comments = $_POST["comments"];
$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Phone: " . $phone . "\n";
$email_body = $email_body . "How did you hear about us?: " . $how . "\n";
$email_body = $email_body . "What sport are you interested in?: " . $sport . "\n";
$email_body = $email_body . "Type of service?: " . $service . "\n";
$email_body = $email_body . "Comments: " . $comments;
echo $email_body;
// TODO: Send Email
header("Location: contact-thanks.php");
// I removed this ^^ slash
?>
Ron Kenzig
4,884 PointsYea, I've tried that to begin with. Still no luck :/
Riley Hilliard
Courses Plus Student 17,771 PointsJust as a test, could you try commenting out the echo? This shouldnt have anything to do with it but just making sure...
Riley Hilliard
Courses Plus Student 17,771 Pointsalso the echo isnt necessary is it? if you are redirecting, there is no need to print anything to the screen
Ron Kenzig
4,884 PointsRiley Hilliard Well, to be honest with you I'm not too sure haha. I've just been going through the videos trying to figure out how to make this contact form work. I'm stuck on this tutorial for now, so I haven't even gotten to the part where it actually sends the information to an email. I removed the echo and it's still not redirecting to the contact-thanks.php
There's got to be something small that I am missing?
Riley Hilliard
Courses Plus Student 17,771 PointsTo test only the redirect, try just this code:
<?php
header("Location: /contact-thanks.php");
?>
or this:
<?php
header("Location: /~sesadmin/contact-thanks.php");
?>
Riley Hilliard
Courses Plus Student 17,771 Points*First section correction:
<?php header("Location: /contact-thanks.php"); ?>
Riley Hilliard
Courses Plus Student 17,771 PointsAlright this form really needs to have an ability to edit a comment Ryan Carson Faye Bridge Tommy Morgan
Ron Kenzig
4,884 PointsSo I replaced all my content in the contact-process.php with the above codes, and even that isn't working? This redirect really hates me!
All was going well in the tutorial, until this... I just don't get it. It seems like an easy task :/
dillonamadeo
6,343 PointsI think the problem lies in the order of the function call. The header function needs to be called before any html is echoed. Try to put the header function before the echo function.
Ron Kenzig
4,884 PointsDillon Amadeo - I just tried to move it before the echo and I'm still having the same issue. I built this site with bootstrap. That shouldn't matter, should it?
Riley Hilliard
Courses Plus Student 17,771 PointsNo, bootstrap shouldnt have an effect on PHP code
Elliott Frazier
Courses Plus Student 9,647 PointsIt would help if you show your form code
Ron Kenzig
4,884 PointsThe form aspect seems to be working correctly, but let me know if something is set up wrong... I'm new to php, and have little experience with the functionality of forms. Here is my "testing" contact page. As you can see the form is outputting to the contact-process.php, but the redirect is not working in the process file.
http://stabilityes.com/contact-2.php
Here is my "thank you page" that I'm trying for life of me to redirect to, and am having no luck.
http://stabilityes.com/contact-thanks.php
And my contact-process.php file is listed above in the thread.
Thanks to all that have helped thus far, beers are on me when this gets figured out ;)
<form method="post" action="contact-process.php">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control" placeholder="Full name">
</div>
<div class="form-group">
<input type="text" name="email" id="email" class="form-control" placeholder="Email address">
</div>
<div class="form-group">
<input type="text" name="phone" id="phone" class="form-control" placeholder="Phone number">
</div>
<div class="form-group">
<input type="text" name="how" id="how" class="form-control" placeholder="How did you hear about us?">
</div>
<p class="form-p">Which sport are you interested in?</p>
<div class="form-group">
<select class="form-control" name="sport" id="sport">
<option></option>
<option>Basketball</option>
<option>Baseball</option>
<option>Soccer</option>
<option>Lacross</option>
<option>Other</option>
</select>
</div>
<p class="form-p">Injury Prevention, Strength & Conditioning, or both?</p>
<div class="form-group">
<select class="form-control" name="service" id="service">
<option></option>
<option>Injury Prevention</option>
<option>Strength & Conditioning</option>
<option>Both</option>
</select>
</div>
<p class="form-p">Comments or Questions? Please, let us know.</p>
<div class="form-group">
<textarea class="form-control" name="comments" id="comments" rows="5"></textarea>
</div>
<button type="submit" class="btn btn-danger center-block">Submit</button>
</form>
Elliott Frazier
Courses Plus Student 9,647 Pointsthis might sound crazy but i've heard that sometimes a button
type of submit does not work as well as a input
Elliott Frazier
Courses Plus Student 9,647 PointsDeleted - (Treehouse should really let us delete comments)
Elliott Frazier
Courses Plus Student 9,647 PointsTotally misread your problem. Please ignore my previous comments.
Milton Centeno
Courses Plus Student 7,940 PointsI have same issue with one of the video lessons "Shirts 4 Mike". It redirects in the video but the exact code on my system does not redirect. The instructor is leaving out some very important information!
Dainis Putans
3,487 Points'''
1. delete "echo" in front of $email_body, "echo $email_body;"
2. Before <?php code write another php code like this:
<?php ob_start(); ?> #3 after you closed your code, at the end of the file with '?>' write this code <?php ob_end_flush(); ?> you should have 3 blocks of php code. '''
Ron Kenzig
4,884 PointsRon Kenzig
4,884 PointsYep, that was it. My mistake, I didn't even notice that they removed the echo command from the previous video. It would of been nice if they told us to remove it and explained why they removed it in the "Redirecting after form submission" video :/
Thanks Elliott Frazier!