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
Michael Paulmeno
6,373 PointsContact Form Errors Out When email field is specified
I am progressing through the PHP track. However the contact form for the "Shirts 4 Mike Page" errors out whenever the e-mail field is filled in correctly. Specifically it says : Notice: Undefined variable: mail in /home/action/workspace/www/contact.php on line 31
Fatal error: Call to a member function AddReplyTo() on a non-object in /home/action/workspace/www/contact.php on line 31
Yet the object is created in line 22. The error does not occur when the field is left blank or filled in with an incorrectly formatted value. It does not seem to be browser specific and occurs regardless of whether or not the message field is filled in.
You should be able to view the site here (although the box will shutoff after some inactivity): http://blistering-ghost-84-138111.use1-2.nitrousbox.com/contact.php
And here's the code for contact.php:
<?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 name, e-mail address, and message.";
exit;
}
foreach($_POST as $value){
if(stripos($value,"Content-Type:") != FALSE){
$error_message = "There was a problem with the information you entered";
exit;
}
}
if($_POST["address"] != "") {
echo "Your form submission has an error.";
exit;
}
require_once("includes/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."Message: ".$message;
$mail->AddReplyTo($email, $name);
$mail->SetFrom($email, $name);
$address = "mjp86@paulmeno.net";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->IsHTML(true); // Set email format to HTML
$mail->MsgHTML($email_body);
$mail->Subject = 'Shirts 4 Mike Contact Form Submission ' . $name;
if(!$mail->Send()) {
echo "There was a problem with your form submission: " . $mail->ErrorInfo;
exit;
}
header("Location: contact.php?status=thanks");
exit;
}
Any help would be much appreciated.
3 Answers
Michael Paulmeno
6,373 PointsAfter making some changes (see OP) the error message does not appear. What happened is I'd gone through the Build a Simple PHP Application course, but did not pay much attention to how to send the form as I do not have a mail server. After starting the Enhancing a Simple PHP Application course I decided to go back and get the contact form working. However it had been so long since doing the first course I couldn't remember what the code should look like and ended up creating a mess instead of a working contact form. That problem has since been corrected.
However it does not send or redirect to the Thank You page when everything is correctly filled out. If something is incorrectly filled out I get a blank screen. It being lunch time I'll work in that later.
Christian Andersson
8,712 PointsA few things I spotted right off the bat:
if ($name== "" OR $email =="" OR $message ="") { has a slight syntax error. You are missing an equal sign at the end of the line (should be ==).
Also, further down the code you have if(stripos($value,"Content-Type:") !== FALSE){ which has an extra equal sign than necessary. This is actually not wrong syntax, but what !== does is a typesafe comparison, which you don't need to do here.
EDIT:
I also think that you got one extra closing bracket } here, but the indentation makes it looks as if it's correct. You are closing your first if statement right after the constructor for the PHPMailer. This means that if either of the variables $name, $email, or $message is blank the constructor will not run and so the code further down will not know what $mail is.
I would try it like this:
if ($name== "" OR $email =="" OR $message =="") {
$error_message = "You must specify a value for name, e-mail address, and message.";
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";
}
}
}
}else{
require_once("includes/phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$email_body ="";
$email_body = $email_body."Name: ".$name."<br>";
$email_body = $email_body. "Email: ".$email ."<br>";
$email_body = $email_body."Message: ".$message;
$mail->AddReplyTo($email, $name);
$mail->SetFrom($email, $name);
$address = "mjp86@paulmeno.net";
$mail->AddAddress($address, "Shirts 4 Mike");
$mail->IsHTML(true); // Set email format to HTML
$mail->MsgHTML($email_body);
$mail->Subject = 'Shirts 4 Mike Contact Form Submission ' . $name;
}
Michael Paulmeno
6,373 PointsI am now not getting the error when the e-mail field is specified, only when the message variable is filled in.
Christian Andersson
8,712 PointsCould you update your post above and include some of the code that was left outside the codebox? Will make it easier to read :)
Michael Paulmeno
6,373 PointsI added the changes plus markdown so everything appears in one box.
Christian Andersson
8,712 Pointscheck my updated reply above.