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

PHP

Freddy Heppell
Freddy Heppell
9,753 Points

No contact form error message

I've just modified the contact form to put everything into a variable, but despite the fact I'm not typing anything in to any of the boxes, I still get redirected to the the error message. Here is my code

<?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, email address, and message.";
    }

    foreach( $_POST as $value ){
        if( stripos($value,'Content-Type:') !== FALSE ){
            $error_message = "There was a problem with the information you entered.";    

        }
    }

    if ($_POST["address"] != "") {
        $error_message = "Your form submission has an error.";

    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    if (!$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 = "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 ="There was a problem sending the email: " . $mail->ErrorInfo;

        }

    }
}

if(isset($error_message)){
    echo $error_message;
}
?>

Any ideas what is wrong?

1 Answer

On the line of code where you have

if (isset($error_message)) {
   $email_body = ""
   // and so on

I think you want to do if (!isset($error_message)) { instead. With your current code, if you leave any of the input fields blank, then $error_message gets set to a string. Then since your block of code that sends the email is inside if (isset($error_message)), all of that code gets executed. The email attempts to send to an empty address (since you didn't fill out that field), and then the error message gets echoed. Since you want the email code not to be executed if there are missing input fields, then you should have if (!isset($error_message)) instead of if (isset($error_message)).