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 trialgyatmnucla
7,618 PointsCan't find error in code
Hello! When, I click the submit it does not redirect to the status=thanks, instead, I get an empty page.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";
$email_body .= "Name: " . $name . "\n";
$email_body .= "Email: " . $email . "\n";
$email_body .="Message: " . $message;
// TODO: Send Email
header("Location: contact.php?status=thanks");
exit;
}
?>
<?php
$pageTitle = "Contact Mike";
$section = "contact";
include("inc/header.php"); ?>
<div class="section page">
<div class="wrapper">
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly.</p>
<?php } else { ?>
<h1>Contact</h1>
<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"); ?>
5 Answers
Connor Glynn
6,381 PointsI just copied and pasted you're code into my project and it worked fine. Your code looks fine as well.
So it can't be a problem with this code specifically (maybes there's an error in your header.php?). Other than that it may be a faulty configuration of your MAMP server.
These are just some suggestions to look into, hope they help :)
miguelcastro2
Courses Plus Student 6,573 PointsCheck your webserver's access/error log for the error message. A blank page does not provide any details on what could be causing the problem. One thing I do suggest is to always check to see if your POST variables are present:
<?php
if (isset($_POST["name"]) ) {
$name = $_POST["name"];
}
// Shorthand
$name = isset($_POST["name"]) ? $_POST["name"] : "N/A";
A better way maybe to replace isset() with empty() to not only check to see if the POST variable has been initialized, but to make sure it is not an empty string.
gyatmnucla
7,618 PointsSorry :( I just started the php track I don't understand what you mean by "Check your webserver's access/error log for the error message. " and what is "check to see if your POST variables are present" for... This is on a local mamp server.
jasonjones5
Courses Plus Student 5,571 PointsI'm suspicious of the .= assigning values to $email_body. Pretty sure that's bad syntax. Try taking out the .
gyatmnucla
7,618 PointsI just tried, no difference...
miguelcastro2
Courses Plus Student 6,573 PointsHey,
You need to learn more about PHP variables to understand why you should check the $_POST array to see if a value is present in a particular index. Also, it is very important to understand how the web server works and where the web server log files are stored. The log files contain the error messages that you need to debug your application and without these basic skills you cannot be an effective developer. I suggest retake some courses here on Team Treehouse regarding PHP arrays and do a search in Goolge for how mamp stores its log files.
gyatmnucla
7,618 PointsYou didn't understand, I started the php track after finishing the front end track It is supposed to start from the beginning so there must be some typo or something because I understood everything he explained and did as he did just it doesn't work :p
Alena Holligan
Treehouse TeacherThere are three ways to show errors.
1) add the following two lines directly below the top <?php (will NOT show parse errors and can still show a blank page)
error_reporting(E_ALL);
ini_set("display_errors", 1);
2) create a file in the root directory of your site named ".htaccess" within that file add the following lines (WILL show parse errors)
# display startup errors
php_flag display_startup_errors on
# display all other errors
php_flag display_errors on
# specify recording of all php errors
php_value error_reporting -1
# html markup of errors, make them easier to read
php_flag html_errors on
3) update your php.ini file to show errors (this WILL show parse errors but may be harder to locate and you don't always have access. Calling "phpinfo();" within a php file will display information about php and help you locate php.ini)
Make sure the following settings are turned on:
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
gyatmnucla
7,618 Pointsgyatmnucla
7,618 Points^^^^^^^^