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 Enhancing a Simple PHP Application Integrating Validation Errors Re-Displaying the Submission

The <textarea> field will not hold and display the value of $message if there is a value in the email <input> field.

the <textarea> default text, held in $message, will not be displayed if there is a value in the email <input> field. Furthermore, the form will submit if there is no message entered... EVEN though we have a conditional at the highest priority specifying an error.

Here is my code:

<?php 

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    // trim removes any white space before and after the variable in case someone typed in a space or a hard return
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);

    // checks is there is a value in each input field
    if ($name == "" OR $email == "" OR $message = "") {
        $error_message = "You must specify a value for the name, email, and message.";
    }

    // Checks that none of the fields has malicious code
    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.";
            }
        }
    }

    // Checks that the spam honeypot field is left blank
    if (!isset($error_message) && $_POST["address"] != "") {
        $error_message = "Your form submission has an error.";
    }

    // Checks that the email is in a valid format
    require_once("inc/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 = "siegetheartist@gmail.com";
        $mail->AddAddress($address, "Siege the Artist");
        $mail->Subject = "Start up Contact Form | " . $name;
        $mail->MsgHTML($email_body);

        if($mail->Send()) {
            header("Location: contact.php?status=thanks"); // redirect
            exit;
        } else {
            $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
        }

    }
}
?>






<?php 
$page_title = "Contact us";
include ("inc/header.php"); 
?>
<section>
<?php 
    // Checks if a $_GET variabe exists. 
    // It exists if values have been sent to the $_GET variable via our form submission. 
    // It then checks the value of our status variable. 
    // If it's set to "thanks" then we want to display this thank you message. 
    // If we do not check the $_GET status exists first, we will get an error message 

    if(isset($_GET["status"]) AND $_GET["status"] == "thanks") { 
        echo '<h1>Email Sent!</h1>';
        echo '<p>Thanks for the email!</p>';
    } else { 

        if (!isset($error_message)) {
            echo '<h1>We\'d love to hear from you!</h1>';
            echo '<p>Send us a message! We always like to hear what the community thinks about the website.</p>';
        } else {
            echo '<h1>Oh oh! Something went wrong.</h1>';
            echo '<p class="#">' . $error_message . '</p>';
            // echo '<pre>' . var_dump($_POST) . '</pre>';
        } 
?>
    <form method="post" action="contact.php">
        <table>
            <tr>
                <th><label for="name">Name</label></th>
                <td><input type="text" name="name" id="name" value="<?php if (isset($name)) { echo $name; } ?>"></td>
            </tr>
            <tr>
                <th><label for="email">Email</label></th>
                <td><input type="text" name="email" id="email" value="<?php if (isset($email)) { echo $email; } ?>"></td>
            </tr>
            <tr>
                <th><label for="message">Message</label></th>
                <td><textarea name="message" id="message"><?php if (isset($message)) { echo $message; } ?></textarea></td>
            </tr>

            <!-- humans dont fill this out -->
            <tr style="display:none;">
                <th><label for="address">Address</label></th>
                <td><input type="text" name="address" id="address" ><p>Do not fill out the address.</p></td>
            </tr>

        </table>
        <input type="submit" value="Send">
    </form>
<?php 
    } // ends the if else statement 
?>
</section>
<?php include ("inc/footer.php"); ?>
Keith Kelly
Keith Kelly
21,326 Points

Carlos do you have a workspace built with this code that you can share? It would be useful if we could fork that space and play with the code.

1 Answer

Daniel Nora
Daniel Nora
2,550 Points

if ($name == "" OR $email == "" OR $message = "")

This condition on your first code snippet uses a single equal sign on the third part, which sets $message to nothing.

Your condition sets $message to nothing whenever $name and $email are not empty, which is probably the root of your problems.

DOH! THat's so obvious now that you point it out. It's always something so simple and so easy to overlook. Thank you for your help. It has been driving me nuts for the past few days.