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

Validating Contact Form Data - problem with validation

Have problem with if statements. On the 6th line there is a if f ($name == "" OR $email == "" OR $message == ""). It is not working. I suppose that it needs to echo error message, but instead, it transfers me to the thank you page (from header fn).

Please help...

           <?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;
}
//security matters
foreach ($_POST as $value) {
    if (stripos($value, 'Content-Type:') !== FALSE){
        echo "There was an error with forms.";
        exit();
    }
}
//fake input field (adress)
if ($_POST["adress"] != "") {
    echo "Form submission have error!";
    exit();
}

$email_body = "";
$email_body .= "Name: ". $name . "\n";
$email_body .= "E mail: ". $email. "\n";
$email_body .= "Message: ". $message. "\n";
//TODO Send e mail
header("Location: contact.php?status=thanks");
exit;
}
?>
<?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>Thank you for sending email. We will answer you shortly</p>
        <?php } else { ?>
        <p>I would love to hear from you! Complete the form to send me an mail.</p>
        <form method="post" action="contact-thanks.php">
            <table>
                <tr>
                    <th>
                        <label for="name">Name</label>
                    </th>
                    <td>
                        <input type="text" name="name" id="name">
                    </td>
                </tr>
                <tr>
                    <th>
                        <label for="emai">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;"> <!-- Fake field only for bots -->
                    <th>
                        <label for="emai">Adress</label>
                    </th>
                    <td>
                        <input type="text" name="adress" id="adress">
                    </td>
                </tr>
            </table>
            <input type="submit" value="send">
        </form>
    </div>
    <?php } ?>
    <?php
    include('includes/footer.php');
    ?>
            ```

I'm researching your problem. Just FYI, using PHP's empty() function will check to see if a variable has an empty value (like 0, false, null, an empty string, etc.). It just keeps your code a little cleaner.

3 Answers

Ok, problem is solved. Believe or not, the problem was windows or xampp itself. When I started to comment code line by line, and do browser refresh, nothing has changed?? I restarted xampp, nothing againg. It just started to work when I reset the pc.

The problem occurs when computer is inserted into sleep mode...

try using strlen function instead like this: if (strlen($name) < 1 OR strlen($email) < 1 OR strlen($message) <1) {

I followed Randy's video, and he demonstrates what will happen if inputs is not filled. He gots error message. I downloaded zip project, compare the codes, and nothing i found wrong....?? Ryan Duchene tnx for tip!