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

Tata Cheng
Tata Cheng
8,787 Points

Enhancing a simple php app > Displaying the error message

By the end of this tutorial, we should be able to display the error message "You must specify a valid email address" if an invalid email address was submitted. However, I keep getting "You must specify a value for name, email and message" for all errors. Can some one please help me, I'm going crazy. I thought I followed everything correctly, but maybe I missed something...

    <?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 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.";
          }
    }
}

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

require_once("includes/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 = "orders@shirts4mike.com";
    $mail->AddAddress($address, "Shirts 4 Mike");
    //Set the subject line
    $mail->Subject = 'Shirts 4 Mike Contact Form Sumbission | ' . $name;
    //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($email_body);    
    //Send the message, check for errors
    if($mail->Send()) {
        header("Location: contact.php?status=thanks");
        exit;
    } else {
      $error_message = "There was an error sending the email: " . $mail->ErrorInfo;
    }

}

}

?> <?php $pageTitle = "Contact Mike"; $section = "contact"; include("includes/header.php"); ?>

<div class="section page">

    <div class="wrapper">

        <h1>Contact</h1>

        <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
        <p>Thanks for the email! I&rsquo;ll be in touch shortly.</p>
        <?php } else { ?>

            <?php
                if (!isset($error_message)) {
                    echo '<p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>';
                    } else {
                    echo '<p class= "message">' . $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">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <label for="email">Email</label>
                        </th>
                        <td>
                            <input type="text"name="email" id="email">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <label for="message">Message</label>
                        </th>
                        <td>
                            <textarea name="message" id="message"></textarea>
                        </td>
                    </tr>
                    <tr style="display: none;">
                        <th>
                            <label for="address">Address</label>
                        </th>
                        <td>
                            <input type="text"name="address" id="address">
                            <p>Humans (and frogs): Please leave this field blank.</p>
                        </td>
                    </tr>
                </table>
                <input type="submit" value="Send">
            </form>

        <?php } ?>
    </div>

</div>

<?php include("includes/footer.php"); ?>
Leslie Hui
Leslie Hui
3,330 Points

Hi Tata,

Why this is happening is because of this line of code

if ($name == "" OR $email == "" OR $message) {
$error_message = "You must specify a value for name, email and message.";
}

If any of the field is missing it will display "You must specify a value for name, email and message.".

Is this what you mean?

Richard Duncan
Richard Duncan
5,568 Points

To expand a little, your current logic is to:-

  1. Check for empty string value
  2. Check for empty string value
  3. Boolean check for true

Because $message is set on line 6 above the 3rd condition is returning true and the variable $error_message is being set each time you run the script. I haven't done this challenge yet but I am guessing the fix is to prepend = "" before the closing parenthesis.

Tata Cheng
Tata Cheng
8,787 Points

Hmm thanks... I will try. But this is the actual lesson, not the challenge which is more frustrating because I followed along yet its not working.

Leslie Hui yes that is the line that shows up. However, it should be saying "you must specify a valid email address" when I type an invalid email.

2 Answers

Pavol Almasi
PLUS
Pavol Almasi
Courses Plus Student 1,524 Points

Does that happen if the email is wrong (such as missing @), or only if the email is not entered at all (empty " ")?

Tata Cheng
Tata Cheng
8,787 Points

It happens in both instances.

Pavol Almasi
Pavol Almasi
Courses Plus Student 1,524 Points

seems like a typo after all. Your first conditional checks if email and name is empty but not the message. you forgot =="" after $message. So $message always validated as true which triggered the error message and set $error_message to true which then skipped all the other conditionals.

Tata Cheng
Tata Cheng
8,787 Points

Sigh... damn typos..

You were right Thank you so much for checking my code, I appreciate it :)

Lol right, gets really frustrating sometimes...damn typos

But great job and good luck! (although I didn't help here)

Tata Cheng
Tata Cheng
8,787 Points

Randy Hoyt do you think you could check my code? Thanks if you can!