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

Why does this code not execute an error message when fields are left blank on Contact page?

<?php

if ($_SERVER["REQUEST METHOD"] == "POST") { $name = trim($_POST["name"]); $email = trim($_POST["email"]); $message = trim($_POST["message"]);

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

}

$email_body = "";
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "Email: " . $email . "\n";
$email_body = $email_body . "Message: " . $message;
Rodrigo Villalobos
Rodrigo Villalobos
2,546 Points

In the second if, I'd make two changes:

First, instead of using $name == "" you can use the PHP function empty like this: if(empty($name)){ echo 'Bla bla bla'; }

Second, instead of using OR use two 'I-don't-know-how-are-they-called: if(empty($name) || empty($email) || empty($message)){ echo 'bla bla'; }

I hope this solves your problem.

if(empty($name))|| (empty($email)) || (empty($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; " }

This code still gives me a server error....any thoughts as to why that would be?

Rodrigo Villalobos
Rodrigo Villalobos
2,546 Points

I think you have a typo instead of !== try just !=

1 Answer

Michael O'Malley
Michael O'Malley
4,293 Points
            <?php

            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                $name = $_POST["name"];
                $email = $_POST["email"];
                $message = $_POST["message"];
                $email_body = "";
                $email_body = $email_body . " Name: " . $name . "\n";
                $email_body = $email_body . " Email: " . $email . "\n";
                $email_body = $email_body . " Message: " . $message;

            if ($_SERVER["REQUEST_METHOD"] == "POST" AND empty($_POST["name"]) OR empty($_POST["email"]) OR empty($_POST["message"])) {
                            echo "You must fill out all fields!"; 
            } else {

                // TODO: Send Email

                header("Location: contact.php?status=thanks"); }
                exit;
            }
            ?>

I'm new to this myself so I can't speak on if this the correct way to handle it, but it seems to work on my end. Hope this helps. :)