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 trialBrian Polonia
25,139 PointsRe-Displaying the Submission - having issue with textarea / message
Hello,
In reference to the video on Re-displaying the submission button, I am having an issue re-displaying the message.
the text inside of the name and email re-display but the text inside of the textarea does not.
At the top of the document my message variable is declared like so:
$message = trim($_POST['message']);
And later down the document where I build the form, my textarea for $message is written as so:
<textarea name="message" id="message"><?php if(isset($message)){ echo $message; } ?></textarea>
Can someone please help me with this? Randy Hoyt maybe?
Thanks!
- Brian
4 Answers
Brad Van Skyhawk
28,489 PointsThe code you posted should work, unless you do not have method="post" in the <form> tag. What does you <form> tag look like?
Brad Van Skyhawk
28,489 PointsSorry, but I cannot replicate your issue. Is the name of file where the form is called contact.php? Is it possible to post the entire file?
carlosjimenez
5,504 PointsI am having the same issue. The message input field does not get saved. Furthermore, it skips in validation. If it is empty the form still submits and is taken to the thank you page.
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
// trim removes any white space before and after the variable in case someone typed in a space or a hard return
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
// checks is there is a value in each input field
if ($name == "" OR $email == "" OR $message = "") {
$error_message = "You must specify a value for the name, email, and message.";
}
// Checks that none of the fields has malicious code
if (!isset($error_message)) {
foreach($_POST as $value) {
if (stripos($value,'Content-Type:') !== FALSE){
$error_message = "There was a problem with the information you entered.";
}
}
}
// Checks that the spam honeypot field is left blank
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
// Checks that the email is in a valid format
require_once("inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!isset($error_message) && !$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;
$mail->SetFrom($email, $name);
$address = "siegetheartist@gmail.com";
$mail->AddAddress($address, "Siege the Artist");
$mail->Subject = "Start up Contact Form | " . $name;
$mail->MsgHTML($email_body);
if($mail->Send()) {
header("Location: contact.php?status=thanks"); // redirect
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
?>
<?php
$page_title = "Contact us";
include ("inc/header.php");
?>
<section>
<?php
// Checks if a $_GET variabe exists.
// It exists if values have been sent to the $_GET variable via our form submission.
// It then checks the value of our status variable.
// If it's set to "thanks" then we want to display this thank you message.
// If we do not check the $_GET status exists first, we will get an error message
if(isset($_GET["status"]) AND $_GET["status"] == "thanks") {
echo '<h1>Email Sent!</h1>';
echo '<p>Thanks for the email!</p>';
} else {
if (!isset($error_message)) {
echo '<h1>We\'d love to hear from you!</h1>';
echo '<p>Send us a message! We always like to hear what the community thinks about the website.</p>';
} else {
echo '<h1>Oh oh! Something went wrong.</h1>';
echo '<p class="#">' . $error_message . '</p>';
}
?>
<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 $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 $email; } ?>"></td>
</tr>
<tr>
<th><label for="message">Message</label></th>
<td><textarea name="message" id="message"><?php if (isset($message)) { echo $message; } ?></textarea></td>
</tr>
<!-- humans dont fill this out -->
<tr style="display:none;">
<th><label for="address">Address</label></th>
<td><input type="text" name="address" id="address" ><p>Do not fill out the address.</p></td>
</tr>
</table>
<input type="submit" value="Send">
</form>
<?php
} // ends the if else statement
?>
</section>
<?php include ("inc/footer.php"); ?>
Mai Lon Ross
5,891 PointsSingle equals sign in this line, setting the $message variable value rather than checking it. if ($name == "" OR $email == "" OR $message = "") {
Brian Polonia
25,139 PointsBrian Polonia
25,139 PointsHere is the complete form:
Please read my comment a few lines above the textarea.
Thanks!