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 trialEiyad Ziyadah
19,250 PointsA problem in redirecting to "status=thanks" page !
I had an issue that prevents redirecting to thanks page, after troubleshooting I found that I did not declare the variable $email_body = "";
and it's fixed after I declare it. can anybody explain why this variable affected my contact submission??
my code before I fix it:
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") {
$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.php?status=thanks");
exit;
}?><?php
$pageTitle = "Contact Mike";
$section = "contact";
<div class="section page">
<div class="wrapper">
<h1>Contact</h1>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
<p>I’d love to hear from you! Complete the form to send me an email.</p>
<form method="post" action="contact.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">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
</div>
</div>
<?php include('inc/footer.php') ?>
6 Answers
Jeff Busch
19,287 PointsWhen I pasted your code into my editor I noticed that the header() statement was on the same line as the comment //, so the header statement was commented out. Also, I commented out $email_body = ""; and that didn't seem to affect anything.
Eiyad Ziyadah
19,250 PointsThanks for the tips Shawn, it will indeed make my code simpler, I will make sure to use these tips in the future.
but I wanted to know why the php block did not execute?? is that because it did not find $email_body
and ignored the whole block??
Shawn Flanigan
Courses Plus Student 15,815 PointsHmm...looks like my above answer didn't really have anything to do with that, did it? I did some testing and while your original code was a bit clunky, it should have still worked. The declaration of the variable must not have been the problem.
Did you do anything else to your code, like move it to the top of the document? I know that if you output anything to the browser before you try to change the header, it won't work.
Eiyad Ziyadah
19,250 PointsThanks guys for help. I ran the code again with $email_body = "";
commented out and I have the page functional. I think it was the comment thing as Jeff said.
Shawn Flanigan
Courses Plus Student 15,815 PointsEiyad,
It looks like your problem was probably coming from the very next declaration:
$email_body = $email_body . "Name: " ...
You're trying to use the value of $email_body
at the same time that you declare it a variable. If you changed that line to:
$email_body = "Name: " ...
you should be able to avoid making an empty declaration first.
On another note...and this shouldn't affect functionality, it's just for the sake of simplicity...later in that block of code, anywhere you write $email_body = $email_body . "Something Else" ...
you can shorten this to read $email_body .= "Something Else" ...
So, that block of code would end up looking like this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "Name: " . $name . "\n";
$email_body .= "Email: " . $email . "\n";
$email_body .= "Message: " . $message;
header("Location: contact.php?status=thanks");
exit;
}
?>
oseijenkins
12,912 PointsI did this exercise and it wasn't redirecting for me even though everything "looked" correct. After a good 30+ mins of going through each line, turns out I had a curly brace too close to the <?php tag. Mine was <?php} else { ?>. The minute I made it <?php } else { ?> it worked like a champ!
Shawn Flanigan
Courses Plus Student 15,815 PointsShawn Flanigan
Courses Plus Student 15,815 PointsI don't see any comments (must have been before the question was edited), but that would certainly do the trick! Good catch.