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 Enhancing a Simple PHP Application Integrating Validation Errors Displaying the Error Message

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Error messages not displaying

I bow once again to the superior wisdom of my fellow Treehouse students :)

I'm 3 minutes and 27 seconds into the video. We're writing the echo stateements so they appear as strings in a variable called $error_message. But they won't appear since I removed the echos. In fact, although my PHP seems to be valid I'm not sure if the form is still wroking as it should.

http://www.jonniegrieve.co.uk/jg-lab.co.uk/treehouse/php/enhanceanapp/contact.php

Here's my code.

<?php 

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


    if ($name == "" OR $email == "" OR $message == "") {
       $error_message = "You must specify a value for name, email address, and message.";        
    }

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

        }
    }

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

    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    if (!$mail->ValidateAddress($email)){
         $error_message =  "You must specify a valid email address.";

    }

    if(!isset($error_message)) {
        $email_body = "";
        $email_body = $email_body . "Name: " . $name . "<br>";
        $email_body = $email_body . "Email: " . $email . "<br>";
        $email_body = $email_body . "Message: " . $message;

        $mail->SetFrom($email, $name);
        $address = "orders@shirts4mike.com";
        $mail->AddAddress($address, "Shirts 4 Mike");
        $mail->Subject    = "Shirts 4 Mike Contact Form Submission | " . $name;
        $mail->MsgHTML($email_body);

        if($mail->Send()) {      
            header("Location: contact.php?status=thanks");
            exit;
        } else {
             $error_messsge = "There was a problem sending the email: " . $mail->ErrorInfo;

        }
    }
}



?><?php 
$pageTitle = "Contact Mike";
$section = "contact";
include('inc/header.php'); ?>

    <div class="section page">

        <div class="wrapper">

            <h1>Contact</h1>

            <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
                <p>Thanks for the email! I&rsquo;ll be in touch shortly!</p>
            <?php } else { ?>



                <?php  
                    if(isset($error_message)) {
                        <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p> 
                        }
                    else {                  
                        echo '<p class="message">' . $error_message . '</p>';                   
                    }
                ?>

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

                    <table>
                        <tr>
                            <th>
                                <label for="name">Name</label>
                            </th>
                            <td>
                                <input type="text" name="name" id="name">
                            </td>
                        </tr>
                        <tr>
                            <th>
                                <label for="email">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;">
                            <th>
                                <label for="address">Address</label>
                            </th>
                            <td>
                                <input type="text" name="address" id="address">
                                <p>Humans (and frogs): please leave this field blank.</p>
                            </td>
                        </tr>                   
                    </table>
                    <input type="submit" value="Send">

                </form>

            <?php } ?>

        </div>

    </div>

<?php include('inc/footer.php') ?>

I'm a little stumped, any ideas? :)

I have the same problem and my code is the same as above. I am very confused because of my technical level, but all things considered the code should be correct, but from what I understand in reading the comments is that the problem simply might be a PHP error.

It is almost NEVER a PHP or Workspace error. Post your code probably has an error. You should post it.

4 Answers

Grace Kelly
Grace Kelly
33,990 Points

Hmm.. i changed your code to what i have above in the above answer and the $error_message works fine now!! Looking at the video I can see that in your conditional statement you are missing a "!" before isset, which is why your statement had me confused haha maybe try changing it to the following:

 <?php  
                    if(!isset($error_message)) {?>
                        <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p> 
                       <?php }
                    else {                  
                        echo '<p class="message">' . $error_message . '</p>';                   
                    }
                ?>

hmm it could be a CSS thing but if they won't appear at all my guess it's a php error!!

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Haha yea as I say I've done up to 3m 27s of the video... i just want to be sure I can display the messages.

To be honest, I might just end up starting the entire stage from scratch. I noticed no messages were displaying before when I changed them from echoes to strings in variables but I just took that mean that because I wasn't echoing any variables. But i haven't been getting any output since last night and i wonder if it'll just be better to rip up the scrap book and start fresh. After all I'm not far through this course. :)

Grace Kelly
Grace Kelly
33,990 Points

Alright!! Sorry I couldn't help you reach a solution, but as I said by including those closing and opening php tags and changing the syntax of the conditional statement the error messages echo out perfectly for me, but sometimes the best thing to do is start fresh! :)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

That's odd, because I made the changes you suggested and they haven't worked. I've run my code through php web apps to flush out errors and it seems to be valid code. :)

Hang on....

Well.. it would help of course... if I uploaded my file to the correct part of the server lol (Been a long busy week)

Your changes seem to have done the trick. Thanks for your help. :D

Grace Kelly
Grace Kelly
33,990 Points

phew!! haha don't worry about it I've made the same mistake, i'm glad it's working for you now! happy coding :)

uploading the the right spot does help. The ! is adding not to the condition, so you get the message if NOT isset. That is what you need. Without the !, you would only get the error message if it was properly set.

Stephen Garner
PLUS
Stephen Garner
Courses Plus Student 11,241 Points

to show any errors on a php script...put these lines of code at the top of your code:

ini_set('display_errors', 1);

ini_set('display_startup_errors', 1);

error_reporting(E_ALL);

Make sure php.ini file is configured to show errors.

Grace Kelly
Grace Kelly
33,990 Points

Hi Jonathan, without really going into your code I can see a minor syntax error here:

  <?php  
                    if(isset($error_message)) {
                        <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p> 
                        }
                    else {                  
                        echo '<p class="message">' . $error_message . '</p>';                   
                    }
                ?>

you are missing closing and opening php tags, try changing it to this:

<?php  
                    if(isset($error_message)) {?>
                        <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p> 
                       <?php }
                    else {                  
                        echo '<p class="message">' . $error_message . '</p>';                   
                    }
                ?>

the error might be just as simple as that :)

Edit: Also what this piece of code is doing is confusing me slightly....if the error message IS set you want the following to be echoed out "I'd love to hear from you!...." but if it ISN'T set you want the error message to be displayed?? Maybe try switching it around to the following:

<?php  
                    if(isset($error_message)) {?>
                        echo '<p class="message">' . $error_message . '</p>'; 
                       <?php }
                    else {                  
                        <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>                   
                    }
                ?>
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Thanks for your reply but sadly it didn't work :)

I wonder if I'm just forgetting that I need to add some CSS for the error to display the way it does in the video but mostly the CSS has been packaged for us so I tend to discount that.

Sorry...bad CSS obstructed my view; altered the CSS and everything is good.