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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Error messages always loads when fields are filled in.

I'm having a bit of trouble with this video. I'm stumped, to be honest.

I've added all the fields, including the video and I keep filling in all the forms but no matter what I do, the errror messages keep appearing. What am I missing? I keep thinking it's because it's taking into account the hidden field but the conditional is not checking that field.

Here's my code.

suggest.php
<?php 


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

    $name = trim(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
    $email = trim(filter_input(INPUT_POST, "email", FILTER_SANITIZE_EMAIL));
    $details = trim(filter_input(INPUT_POST, "details", FILTER_SANITIZE_SPECIAL_CHARS));


    if($name == "" || $email == "" || $details == "") {
        echo "Please fill in the details in all form fields";
        exit;
    }

    if($_POST["address"] != "") {
        echo "Bad form input";
        exit;       
    }

    echo "<pre>";
    $email_body = "";
    $email_body .= "Name: " . $name . "\n";
    $email_body .= "Email: " . $email . "\n";
    $email_body .= "Details: " . $details . "\n";
    echo $email_body;
    echo "</pre>";

    //To do - send email

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

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

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

        <div class="section page">

            <div class="wrapper">

                <h1>Suggest a Media Item</h1>

                <p>Not seeing what you want in our catalog? Let us know!</p>

                <?php if(isset($_GET["status"]) && $_GET["status"] == "thanks") {
                    echo "<p>Thanks for the email,  I&rsquol;ll check out your suggestion shortly!</p>";
                } else { ?>


                <form method="post" action="suggest.php">

                    <table>
                        <tr>
                            <th>
                                <label for="first_name">Name:</label>

                            </th>
                            <td>
                                <input type="text" id="first_name" name="first_name" />
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="email">Name:</label>

                            </th>
                            <td>
                                <input type="text" id="email" name="email" />
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="suggest">Suggestion:</label>

                            </th>
                            <td>
                                <textarea name="suggest" id="suggest" />Send your suggestions here!</textarea>
                            </td>   

                        </tr>
                        <tr style="display: none">
                            <th>
                                <label for="address">Address:</label>

                            </th>
                            <td>
                                <input type="text" name="address" id="address" />
                                <p>Please leave this field blank. You can safely ignore it.</p>
                            </td>   

                        </tr>
                    </table>

                    <input type="submit" value="Send" />        



                </form>
                <?php  } ?>
            </div>

        </div>

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

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Jonathan,

In your conditional, you're checking against the variable $name pulling in the id of "name", but in the table markup, you are using an id of "first_name".

See if that fixes the error.

:dizzy:

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

... another read of it, I also don't see a "details" id. There is only a "suggest". ?

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

You saved the day, thanks :)

I also changed the id of one of the fields to suggest to get it to work. Sometimes you just need that second pair of eyes :)