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
Devin Gray
39,261 PointsForm disappears after submission - devingraymdd.com
My form on devingraymdd.com keeps disappearing when I hit submit whether the form is submitted correctly or not. If filled out correctly, the thanks message doesn't show, and if it isn't filled out correctly, then everything after first name disappears. I could use some help on a fix. Here is the code for my contact page:
<?php //var_dump($_POST); - Posts all of the information of the $_POST variable. ?>
<?php
require_once('../inc/config.php');
/* This file contains instructions for three different states of the form:
* - Displaying the initial contact form
* - Handling the form submission and sending the email
* - Displaying a thank you message
*/
// a request method of post indicates that
// we are receiving a form submission
if ($_SERVER["REQUEST_METHOD"]=="POST" ) {
// the form has fields for name, email, and message
$firstName = trim($_POST[ "first-name"]);
$lastName = trim($_POST["last-name"]);
$name = $firstName . " " . $lastName;
$email = trim($_POST[ "email"]);
$message = trim($_POST[ "message"]);
$emailAddress = "devingray@devingraymdd.com";
// the fields name, email, and message are required
if ($firstName== "" OR $lastName == "" OR $email == "" OR $message == "") {
$errorMessage = "You must specify a value for your name, email address, and message.";
}
// this code checks for malicious code attempting
// to inject values into the email header
if (!isset($errorMessage)) {
foreach($_POST as $value) {
if(stripos($value,'Content-Type:') !== FALSE) {
$errorMessage = "There was a problem with the information you entered.";
}
}
}
// the field named address is used as a spam honeypot
// it is hidden from users, and it must be left blank
if (!isset($errorMessage) && $_POST["username"] != "") {
$errorMessage = "Your form submission has an error.";
}
// if, after all the checks above, there is no message, then we
// have a valid form submission; let's send the email
require_once(ROOT_PATH . 'inc/phpmailer/class.phpmailer.php');
//Create a new PHPMailer instance
$mail = new PHPMailer();
if (!isset($errorMessage) && !$mail -> ValidateAddress($email)) {
$errorMessage = "You must specify a valid email address.";
}
if (!isset($errorMessage)) {;
$emailBody= "";
$emailBody = $emailBody . "Name: " . $name . "<br>";
$emailBody = $emailBody . "Email: " . $email . "<br>";
$emailBody = $emailBody . "Message: " . $message;
?>
<?php
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "";
$mail->Port = 26;
$mail->Username = "";
$mail->Password = "";
?>
<?php
//Set who the message is to be sent from
$mail->setFrom($email, $name);
//Set an alternative reply-to address
$mail->addReplyTo($email, $name);
//Set who the message is to be sent to
$mail->addAddress($emailAddress, $myName);
//Set the subject line
$mail->Subject = "Thank you for contacting me" . $firstName;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML($emailBody);
//Attach an image file
//$mail->addAttachment('images/phpmailer_mini.png');
//send the message, check for errors
if ($mail->send()) {
header("Location: " . BASE_URL . "contact/?status=thanks");
exit;
} else {
$errorMessage = "There was a problem sending your email: " . $mail->ErrorInfo;
}
}
}
?>
<?php $pageTitle="Feel free to contact me" ; $section="contact" ; include(ROOT_PATH . 'inc/header.php'); ?>
<?php if (isset($_GET[ "status"]) && $_GET[ "status"]=="thanks" ) { ?>
<div class="container">
<h3>Thank You</h3>
<p>Thank you for contacting me. I will get in touch with you as soon as possible.</p>
<?php } else { ?>
<!--Contact Primary-->
<section id="primary">
<h3>General Information</h3>
<p>I work full time and I study web design and development in my spare time. Despite what sounds like a hectic schedule (and believe me it is), I am available to take on web design and development jobs.</p>
<p>The best way to reach me is by email, or on Twitter. Calling should be reserved for serious, and urgent inquiries.</p>
</section>
<!--Contact Secondary-->
<section id="secondary">
<h3>Contact Information</h3>
<ul class="contact-info">
<li class="phone"><a href="tel:818-406-9441">1 (818) 406-9441</a>
</li>
<li class="mail"><a href="mailto:devingray@devingraymdd.com">devin.gray92@gmail.com</a>
</li>
<li class="twitter"><a href="http://twitter.com/intent/tweet?screen_name=dmgakabam">@DMGakaBAM</a>
</li>
</ul>
</section>
<!--Contact Form-->
<section class="forms">
<h3>If You Would Like Me To Contact You...</h3>
<?php if (!isset($errorMessage)) { echo '<p>I would love to hear from you, so please fill out the form below and I will get in touch with you as soon as possible</p>'; } else { echo '<p class="message">' . $errorMessage . '</p>'; } ?>
<form method="post" action="<?php echo BASE_URL; ?>contact/">
<div style="display: none">
<input name="username" type="hidden" value="" />
<p>People, if you can see this page on the site, please leave blank.</p>
</div>
<label for="first-name">First Name</label>
<input name="first-name" type="text" id="first-name" value="<?php if (isset($firstName)) {echo htmlspecialcharacters($firstName);} ?>" />
<label for="last-name">Last Name</label>
<input name="last-name" type="text" id="last-name" value="<?php if (isset($lastName)) {echo htmlspecialcharacters($lastName);} ?>" />
<label for="email">E-Mail Address</label>
<input name="email" type="text" id="email" value="<?php if (isset($email)) {echo htmlspecialcharacters($email);} ?>" />
<textarea name="message" id="message">
<?php if (isset($message)) {echo $message;} ?>
</textarea>
<input type="submit" value="Send" class="button">
</form>
</section>
<?php } ?>
</div>
<?php include(ROOT_PATH . 'inc/footer.php'); ?>
1 Answer
Kazimierz Matan
13,257 PointsThis part of code:
if ($mail->send()) {
header("Location: " . BASE_URL . "contact/?status=thanks");
exit;
} else {
$errorMessage = "There was a problem sending your email: " . $mail->ErrorInfo;
}
indicates that you use redirects in your .htaccess. Check if "contact/?status=thanks" is properly redirected to a relevant file.