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 trialAndrew Thompson
28,063 PointsForm redirect to thanks.php
Hi Guys,
I am having a problem with this. I am expecting this code to execute, and the user to be redirected to thanks.php after the data has been posted to process.php. However the code is executing the $email_body echo and remains on process.php
I have checked the code and can't see anything wrong but I am hoping someone out there with a keen eye for detail may have spotted something I have missed.
Thanks
A
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$details = $_POST["details"];
echo "<pre>";
$email_body = '';
$email_body .= "Name " . $name . "\n";
$email_body .= "Email " . $email . "\n";
$email_body .= "Details " . $details . "\n";
echo $email_body;
echo "</pre>";
// To do : Send Email
header("location:thanks.php");
?>
10 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Andrew,
php has something called output buffering. When output buffering is turned on then no output will be sent except for headers. The output is instead stored in a buffer, generally for further processing before it is sent.
The php environment within workspaces has output buffering turned on. This is why it works in the video and the echo statements do not prevent the header redirect from working. You likely have output buffering turned off in your local environment.
To check this, you can run the phpinfo() function inside your script. http://php.net/manual/en/function.phpinfo.php
<?php
phpinfo(INFO_CONFIGURATION);
?>
You can read more about the constant I passed in at the link above. Basically, it's to reduce the amount of output to make it easier to find what you're looking for.
You can run this both locally and in workspaces to see the difference. You want to look for output_buffering in that table.
Or you can look for it in your php.ini file. This is the file where it's initially set.
The output_buffering directive will turn on or off buffering across all files.
You can turn on output buffering as needed within your scripts by calling the ob_start() function. http://php.net/manual/en/function.ob-start.php
So for you to get this working locally with the echo statements in place, you either need to change the output_buffering value to On (4096 seems to be the recommended value) in your php.ini file or you could place a call to ob_start() before your echo statements and that should get it working.
Unsubscribed User
48,988 PointsWorked for me
<?php
ob_start();
$name = $_POST["name"];
$email = $_POST["email"];
$details = $_POST["details"];
echo "<pre>";
$email_body = "";
$email_body .= "Name " . $name . "\n";
$email_body .= "Email" . $email . "\n";
$email_body .= "Details " . $details . "\n";
echo $email_body;
echo "</pre>";
// To Do: Send email
header("location:thanks.php");
ob_flush();
ob_end_clean();
?>
Stu Cowley
26,287 PointsThis worked for me too, thanks Jens!
michaelleonard4
7,013 PointsRemove the echo statements and see if you get redirected to thanks.php
echo "<pre>";
$email_body = '';
$email_body .= "Name " . $name . "\n";
$email_body .= "Email " . $email . "\n";
$email_body .= "Details " . $details . "\n";
echo $email_body;
echo "</pre>";
```
Andrew Thompson
28,063 PointsHi Michael,
I actually copied my code into workspaces and it ran fine as it is! So I think it may be an issue with my local development (MAMP Pro). Not entirely sure why it's happening still however. Both Jeff and your methods work and execute the function how I would expect. I guess I am just curious as to why it works in the corse but not on local development?
like Jeff said in the previous comment:
"The header function does not work if you output anything to the screen before using the header function. So, I believe, since you are echoing before your header function, the header function cannot work."
Thanks for the help, if you know why it's behaving like this on local development i'd love to know!
Jeff Lemay
14,268 PointsThe header function does not work if you output anything to the screen before using the header function. So, I believe, since you are echoing before your header function, the header function cannot work.
Common issues here are people having a space at the top of their file before the opening php tag. Can cause hours of frustrating.
Andrew Thompson
28,063 PointsHi Jeff,
Thanks for this, I had actually tried to put the header function before any other code and it worked as expected. However in the course Build a basic PHP website on the video Redirecting After a Form Submission. The tutor Alena Holligan's code is identical to mine and the function executes for her?
Any ideas?
A
Daniel Brown
16,908 PointsIm having exactly the same issue within the MAMP working environment.
jason chan
31,009 PointsProbably web caching. Clear that browser history. Actually everything has cache. LOLs.
Andrew Thompson
28,063 PointsHi Jason,
I really wanted this to be the answer because its so simple and to be fair wasn't something I had done. However it still isn't working.
Any other suggestions?
A
jason chan
31,009 Pointsit's three files so you really have to becareful. But later on she makes it into one file.
thanks.php
<?php
$pageTitle = "Thank you";
$section = null;
include("inc/header.php");
?>
<div class="section page">
<h1>Thank you</h1>
<p>Thanks for the email! I’ll check out your suggestion shortly!</p>
</div>
<?php include("inc/footer.php"); ?>
suggest.php
<?php
$pageTitle = "Suggest a Media Item";
$section = "suggest";
include("inc/header.php"); ?>
<div class="section page">
<div class="wrapper">
<h1>Suggest a Media Item</h1>
<p>If you think there is something I’m missing, let me know! Complete the form to send me an email.</p>
<form method="post" action="process.php">
<table>
<tr>
<th><label for="name">Name</label></th>
<td><input type="text" id="name" name="name" /></td>
</tr>
<tr>
<th><label for="email">Email</label></th>
<td><input type="text" id="email" name="email" /></td>
</tr>
<tr>
<th><label for="name">Suggest Item Details</label></th>
<td><textarea name="details" id="details"></textarea></td>
</tr>
</table>
<input type="submit" value="Send" />
</form>
</div>
</div>
<?php include("inc/footer.php"); ?>
process.php
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$details = $_POST["details"];
echo "<pre>";
$email_body = "";
$email_body .= "Name " . $name . "\n";
$email_body .= "Email " . $email . "\n";
$email_body .= "Details " . $details . "\n";
echo $email_body;
echo "</pre>";
//To Do: Send Email
header("location:thanks.php");
?>
try that.
To best honest you can skip that part. Just start follow the part where she brings in the third party library mailerphp. You only need headers if you plan to redirect a landing page or someone does a bad login. But all that stuff is written in laravel or other php frameworks.
Alar Sillaots
Courses Plus Student 845 Pointsremoved from process.php
echo "<pre>"; echo $email_body; echo "</pre>";
and it worked for me
Kyle Bilton
7,111 PointsOkay, this is driving me crazy.
I've been following this thread, tested out a few of the recommended fixes but I can NOT get the header function to work and the page to redirect. If anyone could help that would be great! Happy to attach some code.
Im using MAMP as my local server. I have applied everything that she's written to my personal project. I've tried adding in ob_start(); at the top of the opening php tag. (Unsure if this is the correct spot). I've also run the phpinfo() function and can confirm the "output_buffering" has "no value", and I know it needs be "4096". There are no spaces in my code.
To whoever can help me out will get my utmost respect. =) Thanks in advance.
Andrew Thompson
28,063 PointsHi Kyle,
This was a while ago, but I remember it driving me insane! The answer from Jason Anello did fix the problem for me. You need to place a call to ob_start() before your echo statements, and that should get it working.
I am not sure if I kept my code from this assignment or I would have taken a look.
Hope this helps!
Kyle Bilton
7,111 PointsThanks for responding Andrew. Do you mean attach ob_start() to each and everyone of my echo statements, because I just added it at the top buts it refuses to work. If you can see any other errors feel free to let me know. I've attached the code below for reference, this is the final product of the "Adding a form" course, i've simply taken out my person email address and password.
<!-- CONTACT FORM PHP PROCESS -->
<?php
ob_start();
//Import the PHPMailer class into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
require '../vendor/phpmailer/src/PHPMailer.php';
require '../vendor/phpmailer/src/Exception.php';
require '../vendor/phpmailer/src/SMTP.php';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim(filter_input(INPUT_POST,"name", FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST,"email", FILTER_SANITIZE_EMAIL));
$message = trim(filter_input(INPUT_POST,"message"));
if ( !$name || !$email || !$message ) {
http_response_code(400);
echo '<h4 class="error"> All Fields Required. </h4>
<a href="../php/contact.php" class="">Go back and try again</a>';
exit;
}
if ($_POST["address"] != "") {
echo "Evil form input!";
exit;
}
if (!PHPMailer::validateAddress($email)) {
echo "Invalid Email Address";
exit;
}
$email_body = "";
$email_body .= "Name: " . $name . "\n";
$email_body .= "Email: " . $email . "\n";
$email_body .= "Message: " . $message . "\n";
// To Do: Send email
$mail = new PHPMailer;
//Tell PHPMailer to use SMTP
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "_____";
//Password to use for SMTP authentication
$mail->Password = "_____";
//It's important not to use the submitter's address as the from address as it's forgery,
//which will cause your messages to fail SPF checks.
//Use an address in your own domain as the from address, put the submitter's address in a reply-to
$mail->setFrom('_____', $name);
$mail->addAddress('_____', 'Kyle _____');
$mail->addReplyTo($email, $name);
$mail->Subject = 'Website enquiry from ' . $name;
$mail->Body = $email_body;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
//REDIRECT TO NEW PAGE
header("location:contact.php?status=thanks");
}
Kyle Bilton
7,111 PointsI got it working! YAY!, ended up changing the mamp/bin/php.ini files!
Andrew Thompson
28,063 PointsAndrew Thompson
28,063 PointsThanks Jason! learnt something new there! I really appreciate the time you and everyone else took to help me out!
Thanks Guys!
A
Wojciech Zaorski
16,387 PointsWojciech Zaorski
16,387 PointsThanks Jason, that's the solution I was looking for!