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
J.C. Hiatt
9,393 PointsUsing Contact Script from Learning Adventure, Getting Error
I'm using a slightly modified version of Randy Hoyt's code and in all cases I am "You must specify a valid name, email address, and message" returned. What's up? I'll also include my form code.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["contact-name"]);
$email = trim($_POST["contact-email"]);
$message = trim($_POST["contact-regular-message"]);
$os = trim($_POST["contact-os"]);
$browser = trim($_POST["contact-browser"]);
$bugs = trim($_POST["contact-report"]);
}
if ($name == "" OR $email == "" OR $message == "") {
echo "You must specify a value for name, email address, and message.";
exit;
}
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
echo "There was a problem with the information you entered.";
exit;
}
}
if ($_POST["address"] != "") {
echo "Your form submission has an error.";
exit;
}
require_once("wp-content/themes/indiebundles/inc/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
if (!$mail->ValidateAddress($email)){
echo "You must specify a valid email address.";
exit;
}
$email_body = "";
$email_body = $email_body . "Name: " . $name . "<br>";
$email_body = $email_body . "Email: " . $email . "<br>";
$email_body = $email_body . "OS:" . $os . "<br";
$email_body = $email_body . "Browser:" . $browser . "<br>";
$email_body = $email_body . "Message: " . $message . "<br>";
$email_body = $email_body . "Bug Report:" . $bugs;
$mail->SetFrom($email, $name);
$address = "noreply@indiebundl.es";
$mail->AddAddress($address, "IndieBundles");
if(isset($message)) {
$mail->Subject = "IndieBundles Contact Form Submission From" . $name;
} elseif(isset($bugs)) {
$mail->Subject = "IndieBundles Bug Report Submission From " . $name;
}
$mail->MsgHTML($email_body);
if(!$mail->Send()) {
echo "There was a problem sending the email: " . $mail->ErrorInfo;
exit;
}
echo "Success!";
// header("Location: localhost/indiebundles/");
// exit;
?>
Form Code:
<div class="modal" id="contact-modal">
<div class="close-button" data-icon=""></div>
<h2>Contact IndieBundles</h2>
<form id="contact" action="mail.php" method="POST">
<label>Name*</label>
<input type="text" name="contact-name" placeholder="Full name">
<label>Email Address*</label>
<input type="email" name="contact-email" placeholder="example@gmail.com">
<label>Message Type*</label>
<select>
<option>Regular Message</option>
<option>Bug Report</option>
</select>
<div class="contact-regular-message">
<label>Message*</label>
<textarea name="contact-message" placeholder="What would you like to say?"> </textarea>
</div><!-- // .contact-regular-message -->
<div class="contact-bug-report">
<label>Operating System*</label>
<input type="text" name="contact-os" placeholder="Mac OS X, Windows 8, etc.">
<label>Browser*</label>
<input type="text" name="contact-browser" placeholder="Safari, Chrome, Firefox, etc.">
<label>Report*</label>
<textarea name="contact-report" placeholder="What seems to be the problem?"> </textarea>
</div><!-- // .contact-bug-report -->
<div class="spamzbeware" style="display:none;">
<input type="text" name="address" id="address">
<p>Humans, please leave this field blank.</p>
</div>
<button type="submit" value="Submit" name="signup-submit">Submit</button>
</form><!-- // #contact -->
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<p>Thanks for contacting us!</p>
<?php } else {}; ?>
</div><!-- // #contact-modal -->
1 Answer
Andy Crofford
9,959 PointsI tested it on my server and it works if you put spaces in the quotes on this line
if ($name == "" OR $email == "" OR $message == "")
Change to:
if ($name == " " OR $email == " " OR $message == " ")