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 Build a Basic PHP Website (2018) Adding a Basic Form Validating Form Data

Alexander Bogdanov
Alexander Bogdanov
2,587 Points

Error message loads even when all the fields are filled in!

So this is my code:

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim(filter_input(INPUT_POST, "name", SANITIZE_STRINGS));
    $email = trim(filter_input(INPUT_POST, "email", SANITIZE_EMAIL));
    $details = trim(filter_input(INPUT_POST, "name", SANITIZE_SPECIAL_CHARS));

    if($name == "" OR $email == "" OR $details = "") {
        echo "Please fill in the required fields: Name, Email and Details";
        exit;
    }

    if($_POST["spam"] != "") {
        echo "We don't appreciate spam bots!";
        exit;
    }

    header("location:suggest.php?status=thanks");
    exit;
}

$pageTitle = "Suggest a Media Item";
$section = "suggest";

include("inc/header.php"); ?>

<div class="section page">
    <div class="wrapper">
        <h1>Suggest a Media Item</h1>
        <?php
        if(isset($_GET["status"]) && $_GET["status"] == "thanks") {
            echo "<p>Thanks for the email! I&rsquo;ll check out your suggestion shortly!</p>";
        } else {
        ?>
        <p>If you think there is something I&rsquo;m missing, let me know! Complete the form to send me an email.</p>
        <form method="post" action="suggest.php">
            <table>
                <tr>
                    <th><label for="name">Name</label></th>
                    <td><input type="text" id="name" name="name" /></td>
                </tr>
                <tr>
                    <th><label for="email">Email</label></th>
                    <td><input type="email" id="email" name="email" /></td>
                </tr>
                <tr>
                    <th><label for="details" />Suggest Item Details</label></th>
                    <td><textarea name="details" id="details"></textarea></td>
                </tr>
                <tr style="display: none;">
                    <th><label for="spam" />Spam Check</label></th>
                    <td><textarea name="spam" id="spam"></textarea></td>
                    <p>Please leave this field blank. This way we detect spam bots</p>
                </tr>
            </table>
            <input type="submit" value="Send" />
        </form>
        <?php } ?>
    </div>
</div>

<?php include("inc/footer.php"); ?>

Even when I fill in all my required fields the error message gets displayed!!

2 Answers

Simon Coates
Simon Coates
28,694 Points

This code fragment shows the main errors.

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));//changed
    $email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL));//changed
    $details = trim(filter_input(INPUT_POST, "details", FILTER_SANITIZE_SPECIAL_CHARS)); //changed

    if($name == "" OR $email == "" OR $details == "") { //changed
        echo "Please fill in the required fields: Name, Email and Details";
        exit;
    }
//etc

The constants you referenced didn't seem to exist. Your detail var used a name value. And $details = "" is assignment, not comparison. you need ==.

Simon Coates
Simon Coates
28,694 Points

don't know, but:

$details = trim(filter_input(INPUT_POST, "name", SANITIZE_SPECIAL_CHARS));

is wrong. it reloads the name variable.

Alexander Bogdanov
Alexander Bogdanov
2,587 Points

how is it supposed to be then? Because it's like that in the tutorial.

Simon Coates
Simon Coates
28,694 Points

details is looking for a parameter called "name" (the second parameter). Given that the name variable already looked for a parameter called name, it seems likely that this is a mistake, and that you should probably have something like:

$details = trim(filter_input(INPUT_POST, "details", SANITIZE_SPECIAL_CHARS));

(i don't know if this is THE problem, but is seemed like it might be A problem)