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 Simple PHP Application Wrapping Up The Project Sending The Contact Form Email

Henry Akinola
Henry Akinola
6,444 Points

I cant get the thanks page to load after the message is sent.

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

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

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

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

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

    if (!isset($error_message) && !$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 . "Enquiry: " . $option . "<br>";
        $email_body = $email_body . "Message: " . $message;


        $mail->SetFrom($email, $name);
        $address = "h*******@gmail.com";
        $mail->AddAddress($address, "Administrator");
        $mail->Subject = "Dynamics | ". $name;
        $mail->MsgHTML($email_body);


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

}

}

?>

<?php $pageTitle = "Contact Us"; include("includes/header.php"); ?> <div class="section_page" id="footer-break">

    <div class="container">
    <div class="row">
            <div class="col-md-8 col-md-push-2">

        <h1>Contact</h1>

        <?php if (isset($_GET["status"]) AND $_GET["status"] =="thanks") { ?>
            <p>Thanks For The Email! W&rsquo;ll Be In touch Shortly!</p>
        <?php } else { ?>

            <?php
                if (!isset($error_message)) {
                    echo '<p>If You Wish to Contact Us, Please Complete The Form Below:</p>';
                } else {
                    echo '<span class= "alert alert-danger">' . $error_message . '</span> <br/><br/>';
                }
            ?>
            </div>
    </div>

5 Answers

Do you get any error messages? Make sure the file names are correct

Henry Akinola
Henry Akinola
6,444 Points

File names are correct and error messages show, when the form is not completed. Also the contact us page is able to send the email but loads a white screen once the message is sent rather then loading the status page.

When you send an email, does it redirect you to contactus.php?status=thanks ? The only difference I can see in your code and the project Code is that your contact form is named "contactus.php" while in the project it is named "contact.php".

Henry Akinola
Henry Akinola
6,444 Points

The file name is called contactus.php. The url doesn't show the ?status=thanks page when redirected.

Henry Akinola
Henry Akinola
6,444 Points

I forgot to add to include the config file thats why it never worked! lol Thanks for your help!