Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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. :)