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 trial
Ismael Carbajal
5,922 PointsValidation message not showing up, extra credit php
Can't figure out why my code is still not displaying the 'thank you message' after the correct input has been entered. Any hints?
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// get the contact form data for each of the fields
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
$error_message=array();
// check if fields are blank
if ($name == "" OR $email == "" OR $message == "") {
$error_message[0] = "You must specify a value for name, email address, and message.";
}
///check that none of the fields have malicious code
foreach($_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
$error_message[1] = "There was a problem with the information you entered.";
}
}
///spam honeypot is left blank
if ($_POST["address"] != "") {
$error_message[2] = "Your form submission has an error.";
}
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
///check that email is in a valid format
if (!$mail->ValidateAddress($email)){
$error_message[3] = "You must specify a valid email address.";
}
//// if its not set, send the email
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;
$mail->SetFrom($email, $name);
$address = "orders@shirts4mike.com";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
$mail->MsgHTML($email_body);
if($mail->Send()) {
header("Location: contact.php?status=thanks");
exit;
} else {
$error_message[4] = "There was a problem sending the email" . $mail->ErrorInfo;
}
}
}
?>
<?php
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>
<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 { ?>
<?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 . '</p>';
// }
foreach ($error_message as $error) {
echo '<p class="message">' . $error . '</p>';
exit;
}
?>
<form method="post" action="contact.php">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name" value="<?php if (isset($name)){echo htmlspecialchars($name);}?>">
</td>
</tr>
<tr>
<th>
<label for="email">Email</label>
</th>
<td>
<input type="text" name="email" id="email" value="<?php if (isset($email)) {echo htmlspecialchars($email);}?>">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"><?php if (isset($message)) {echo htmlspecialchars($message);} ?></textarea>
</td>
</tr>
<tr style="display: none;">
<th>
<label for="address">Address</label>
</th>
<td>
<input type="text" name="address" id="address">
<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') ?>
Ismael Carbajal
5,922 PointsSo I fill out the form correctly and I don't get this message:
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for the email! I’ll be in touch shortly!</p>
<?php } else { ?>
Jonathon Grant
21,526 Pointsthe output message should be echoed out by your PHP if statement conditional:
<?php
if ( (isset($_GET["status"])) && ($_GET["status"] == "thanks") ) {
echo "<p>Thanks for the email! I'll be in touch shortly!</p>";
} else {
exit;
}
?>
Jason Anello
Courses Plus Student 94,610 PointsHi Ismael,
After filling out the form correctly and hitting submit, what do you see on the page and what does the url look like?
1 Answer
Jack Blankenship
Full Stack JavaScript Techdegree Graduate 39,036 PointsIt might be that you have declared $error_message as an array that is causing the if(!isset($error_message()) to fail.
I tend to stick echo statements around if clauses that are potential issues.
Muhammad Mohsen
Courses Plus Student 10,843 PointsMuhammad Mohsen
Courses Plus Student 10,843 PointsWhere is 'thank you message' exactly in your code?
you should get it only if the if($mail->Send() conditional executed