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


Form - Errors display on form.

Hey all, I have the form pasted below and it works perfect but I need the errors to show up on the form not on a blank screen. I have done this before on another form by creating an empty errors array and adding errors to it and then using isset to display them but for some reason it fails. This is the working version with a blank screen of erros showing.

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = trim($_POST["name"]);
        $email = trim($_POST["email"]);
        $phone = trim($_POST["phone"]);
        $message = trim($_POST["message"]);
        $package = $_POST["package"];

        if ($name == "" OR $email == "" OR $message == "") {
            echo "You must enter your Name, Email and a      message.";

        foreach ( $_POST as $value ) {
            if( stripos($value, 'Content-Type') !== FALSE ) {
                echo "There was a problem with the information you entered"; 

        if ($_POST["website"] != "") {
            echo "Your form submission has an error.";

        $mail = new PHPMailer();

         if (!$mail->ValidateAddress($email)) {
            echo "You must specity a valid email adrress.";

        $email_body = "";
        $email_body = $email_body . "Name: " . $name . "<br>";
        $email_body = $email_body . "Email: " . $email . "<br>";
        $email_body = $email_body . "Phone: " . $phone . "<br>";
        $email_body = $email_body . "Package: " . $package . " <br>";
        $email_body = $email_body . "Messgae: " . $message;

        $mail->SetFrom($email, $name);
        $address = "edgeofcinema@gmail.com";
        $mail->AddAddress($address, "Edge of Cinema");
        $mail->Subject = "Edge of Cinema" . $name;

        if(!$mail->Send()) {
            echo "There was a problem sending the email: " . $mail->ErrorInfo;
        header("Location: contact.php?status=thank-you");

    <?php include 'includes/head-inc.php'; ?>

    <?php if (isset($_GET["status"]) AND $_GET["status"] == "thank-you" ) { ?>
    <div class="container">
        <section style="padding: 0 60px;" class="col-lg-8 col-lg-offset-2 text-center">
            <h2 style="color: #b6b5b5; margin-top: 170px;">Thank You</h2>
            <h3 style="color: #b6b5b5; margin-bottom: 170px;">Your message has been received and is important to us and we aim to respond 
                within 72hrs. Should you need to speak to us please call 1-855-831-1726</h3>
    <?php } else { ?>

    <header id="contact">
        <div class="container highText text-center">
            <h1>Contact Us</h1>
                <section id="hours">
                    <div class="row">
                        <div class="col-lg-6 col-lg-offset-2">
                            <p style="margin: 6px 0;">PHONE - 1-855-831-1726</p>
                            <p style="margin: 6px 0;">EMAIL -    EDGEOFCINEMA@GMAIL.COM</p>
                        </div><!-- col-lg-8 close -->
                        <div class="col-lg-2">
                            <p style="text-align: center">HOURS</p>
                            <p style="text-align: center">MON - FRI</p>
                            <p style="text-align: center">9AM - 5PM</p>
                        </div><!-- col-lg-8 close -->
        </div><!-- Container Close -->

    <section id="cform">
        <div class="container">
            <form method="post" action="contact.php">
                <h2>Video Production Notes</h2>
                    <div class="fgroup">
                        <label>Name: </label>
                            <input name="name" type="text">
                        <label>Phone: </label>
                            <input name="phone" type="phone">
                        <label>Email: </label>
                            <input name="email" type="email">
                        <label style="display: none;">Website:  </label>
                            <input style="display: none;"  name="website" type="text">
                    </div><!-- fgroup Close -->
                    <div class="fgroup">
                        <label>Describe your video project: </label>
                            <textarea name="message" type="textarea" placeholder="Here's what I need&hellip;"></textarea>
                        <label for="package">Select a Package: 
                        <select name="package" id="package">
                            <option <?php if  ($_GET['option']=='fd') echo 'selected'; ?> value="Full-Day">Full Day</option>
                            <option <?php if ($_GET['option']=='hd') echo 'selected'; ?> value="Half-Day">Half Day</option>
                            <option <?php if ($_GET['option']=='custom') echo 'selected'; ?> value="Custom">Custom</option>

                        <button class="btn" style="color: white" type="submit">Send Message</button>
                    </div><!-- fgroup Close -->
        </div><!-- Container Close -->
    <?php } ?>

    <?php include 'includes/foot-inc.php'; ?>


1 Answer


One thing you can do is put the form in another file and include that file in the script that handles the input. Then, in the input handle script you can use if($_POST) to handle your user's input. During the validation you can have initialized an empty error array and add errors to that array. Then at the bottom you can have a conditional that states if there are no errors in the array, handle the input and either display a message or forward to another page. If there are errors then it jumps out of the if($_POST) conditional and displays the form like it would by default and you would have all your errors to display at your pleasure. Try to build the page like I described and see if it works for you. If you need help I can give you some examples but the fun in learning is to see if you can do it yourself and ask for help later. Good luck.


Totally agree with the last comment. Often when stuck I would prefer to be pointed in the right direction and let me explore with trial and error. Will try what you suggested and report back thanks :)