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 trialMichael Lazarz
15,135 PointsSubmitting contact form after making changes, now displays blank screen
After making the changes in the Setting error Message variables. When I submit an invalid form it just takes me to a blank contact.php page
I checked and I am not receiving any errors when I compile and run code.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == ""){
$error_message = "You must specify a value for a name, email and message.";
}
foreach ($_POST as $value) {
if(stripos($value, 'Content-Type:') !== FALSE){
$error_message = "There was a problem with the information you entered.";
}
}
if ($_POST["address"] != ""){
$error_message = "Your form submission has an error.";
}
require_once ("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$mail->ValidateAddress($email);
if (!$mail->ValidateAddress($email)){
$error_message = "You must specify a valid email address.";
}
if ( !isset($error_message)){
$email_body = " ";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;
// Test
// echo $email_body;
// TODO: Send Email
$mail->SetFrom($email, $name);
$address = "orders@shirts4mike.com";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
// $mail->AltBody = "To view the message, please use an HTML compatible email viewer";
$mail->MsgHTML($email_body);
// $mail->AddAttachment("img/phpmailer.gif");
// $mail->AddAttachment("img/phpmailer_mini.gif");
if ($mail->Send() ) {
// This redirects user ot this page
// adding a variable after address are sent to server alongwith the wbpage
// avaible using $_GET
// ?status=thanks
header("Location: contact.php?status=thanks");
exit;
}else {
$error_message = "There was a problem sending your email: " . $mail->ErrorInfo;
}
} // end if
}
if (isset($error_message)){
echo $error_message;
}
?><?php
$pageTitle = "Contact Mike";
$section = "contact";
include ('inc/header.php'); ?>
<div class="section page">
<div class="wrapepr">
<h1>Contact</h1>
<?php if (isset ($_GET["status"] ) AND $_GET["status"] == "thanks") { ?>
<p>Thank you for the email!</p>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<p>I’d love to hear from you! Complete the form to send me an email</p>';
}else {
echo '<p class="message">' . $error_message;
}
?>
<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>
<tr style="display: none">
<th>
<label for="address">Address: </label>
</th>
<td>
<textarea name="address" id="address"></textarea>
<p>Humans (and frogs): please leave this field blank.</p>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
</div>
</div>
<?php include ('inc/footer.php'); ?>
2 Answers
Hugo Paz
15,622 PointsHi Michael,
You have a curly brace in the wrong place:
header("Location: contact.php?status=thanks");
exit;
}else {
$error_message = "There was a problem sending your email: " . $mail->ErrorInfo;
}
} // end if
}
It should be:
header("Location: contact.php?status=thanks");
exit;
}
}else {
$error_message = "There was a problem sending your email: " . $mail->ErrorInfo;
}
}
Michael Lazarz
15,135 PointsJust tried it and same result
Hugo Paz
15,622 PointsThis issue may lie with this line,
require_once ("inc/phpmailer/class.phpmailer.php");
To test your code i did this:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == ""){
$error_message = "You must specify a value for a name, email and message.";
}
foreach ($_POST as $value) {
if(stripos($value, 'Content-Type:') !== FALSE){
$error_message = "There was a problem with the information you entered.";
}
}
if ($_POST["address"] != ""){
$error_message = "Your form submission has an error.";
}
// require_once ("inc/phpmailer/class.phpmailer.php");
// $mail = new PHPMailer();
// $mail->ValidateAddress($email);
// if (!$mail->ValidateAddress($email)){
// $error_message = "You must specify a valid email address.";
// }
if ( !isset($error_message)){
$email_body = " ";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "Message: " . $message;
// Test
// echo $email_body;
// TODO: Send Email
$mail->SetFrom($email, $name);
$address = "orders@shirts4mike.com";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
// $mail->AltBody = "To view the message, please use an HTML compatible email viewer";
$mail->MsgHTML($email_body);
// $mail->AddAttachment("img/phpmailer.gif");
// $mail->AddAttachment("img/phpmailer_mini.gif");
if ($mail->Send() ) {
// This redirects user ot this page
// adding a variable after address are sent to server alongwith the wbpage
// avaible using $_GET
// ?status=thanks
header("Location: contact.php?status=thanks");
exit;
}
}else {
$error_message = "There was a problem sending your email: " . $mail->ErrorInfo;
}
}
if (isset($error_message)){
echo $error_message;
}
?><?php
$pageTitle = "Contact Mike";
$section = "contact";
include ('inc/header.php'); ?>
<div class="section page">
<div class="wrapepr">
<h1>Contact</h1>
<?php if (isset ($_GET["status"] ) AND $_GET["status"] == "thanks") { ?>
<p>Thank you for the email!</p>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<p>I’d love to hear from you! Complete the form to send me an email</p>';
}else {
echo '<p class="message">' . $error_message;
}
?>
<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>
<tr style="display: none">
<th>
<label for="address">Address: </label>
</th>
<td>
<textarea name="address" id="address"></textarea>
<p>Humans (and frogs): please leave this field blank.</p>
</td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php } ?>
</div>
</div>
<?php include ('inc/footer.php'); ?>
Nathan Beste
28,366 PointsYou should check to see if your top address is actually changing over to "http://localhost/contact.php?status=thanks". What fixed it for me was adding ob_start(); before you do any includes.
<?php ob_start(); include ('inc/header.php'); ?>
Michael Lazarz
15,135 PointsMichael Lazarz
15,135 PointsHugo. Not entirely sure what was wrong but it started working. Thank you for the help. I think it was that line