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

Contact Form Problem for My Personal Page

I've been working on a portfolio site for myself, it's not nearly finished yet, but I've been stuck on something recently.

I've been trying to put a form on my website instead of just having my email and phone number just sitting there on my site, so I went back and went over the "Enhancing a Simple PHP Application" videos to get some help, as far as I can tell, I've written all of the code perfectly for it to work, but it keeps displaying a "Your form submission has an error." message when I hit send and all the parameters are met.

The website is devingraymdd.com so you can test it out yourself.

Here is the code for the contact page:

<?php //var_dump($_POST); - Posts all of the information of the $_POST variable. ?>
<?php
require_once('../inc/config.php');
/* This file contains instructions for three different states of the form:
 *   - Displaying the initial contact form
 *   - Handling the form submission and sending the email
 *   - Displaying a thank you message
 */

// a request method of post indicates that
// we are receiving a form submission
if ($_SERVER[ "REQUEST_METHOD"]=="POST" ) {

    // the form has fields for name, email, and message
    $firstName = trim($_POST[ "first-name"]);
    $lastName = trim($_POST["last-name"]);
    $name = $firstName . " " . $lastName;
    $email = trim($_POST[ "email"]);
    $message = trim($_POST[ "message"]);
    $emailAddress = "devin.gray92@gmail.com";
    // the fields name, email, and message are required
    if ($name == "" OR $email == "" OR $message == "") {
        $errorMessage = "You must specify a value for name, email address, and message.";
    }
    // this code checks for malicious code attempting
    // to inject values into the email header
    if (isset($errorMessage)) {
        foreach($_POST as $value) {
            if(stripos($value,'Content-Type:') !== FALSE) {
                $errorMessage = "There was a problem with the information you entered.";
            }
        }
    }
    // the field named address is used as a spam honeypot
    // it is hidden from users, and it must be left blank
    if (!isset($errorMessage) && $_POST["username"] != "") {
        $errorMessage = "Your form submission has an error.";
    }
    // if, after all the checks above, there is no message, then we
    // have a valid form submission; let's send the email
    require_once(ROOT_PATH . 'inc/phpmailer/class.phpmailer.php');
    //Create a new PHPMailer instance
    $mail = new PHPMailer();
    if (!isset($errorMessage) && !$mail -> ValidateAddress($email)) {
        $errorMessage = "You must specify a valid email address.";
    }
    if (!isset($errorMessage)) {;
        $emailBody= "";
        $emailBody = $emailBody . "Name: " . $name . "<br>";
        $emailBody = $emailBody . "Email: " . $email . "<br>";
        $emailBody = $emailBody . "Message: " . $message;
?>

<?php
        //Set who the message is to be sent from
        $mail->setFrom($email, $name);
        //Set an alternative reply-to address
        $mail->addReplyTo($email, $name);
        //Set who the message is to be sent to
        $mail->addAddress($emailAddress, $myName);
        //Set the subject line
        $mail->Subject = "Thank you for contacting me" . $firstName;
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        $mail->msgHTML($emailBody);
        //Attach an image file
        //$mail->addAttachment('images/phpmailer_mini.png');

        //send the message, check for errors
        if ($mail->send()) {
            header("Location: " . BASE_URL . "contact/?status=thanks");
            exit;
        } else {
            $errorMessage = "There was a problem sending your email: " . $mail->ErrorInfo;
        }

    }
}
?>

<?php $pageTitle="Feel free to contact me" ; $section="contact" ; include(ROOT_PATH . 'inc/header.php'); ?>


<?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") { ?>
    <h3>Thank You</h3>
    <p>Thank you for contacting me. I will get in touch with you as soon as possible.</p>
<?php } else { ?>
        <!--Contact Primary-->
    <section id="primary">
        <h3>General Information</h3>
        <p>I work full time and I study web design and development in my spare time. Despite what sounds like a hectic schedule (and believe me it is), I am available to take on web design and development jobs.</p>
        <p>The best way to reach me is by email, or on Twitter. Calling should be reserved for serious, and urgent inquiries.</p>
    </section>
    <!--Contact Secondary-->
    <section id="secondary">
        <h3>Contact Information</h3>
        <ul class="contact-info">
            <li class="phone"><a href="tel:818-406-9441">1 (818) 406-9441</a>
            </li>
            <li class="mail"><a href="mailto:devin.gray92@gmail.com">devin.gray92@gmail.com</a>
            </li>
            <li class="twitter"><a href="http://twitter.com/intent/tweet?screen_name=dmgakabam">@DMGakaBAM</a>
            </li>
        </ul>
    </section>

    <!--Contact Form-->

    <section class="forms">
        <h3>If You Would Like Me To Contact You...</h3>
        <?php if (!isset($errorMessage)) {
            echo '<p>I would love to hear from you, so please fill out the form below and I will get in touch with you as soon as possible</p>';
        } else {
            echo '<p class="message">' . $errorMessage . '</p>';
        } ?>
        <form method="post" action="<?php echo BASE_URL; ?>contact/">
            <div style="display: none">
                <input name="username" type="hidden" value="username" />
                <p>People, if you can see this page on the site, please leave blank.</p>
            </div>
            <label for="first-name">First Name</label>
            <input name="first-name" type="text" id="first-name" value="<?php if (isset($firstName)) {echo htmlspecialcharacters($firstName);} ?>"/>
            <label for="last-name">Last Name</label>
            <input name="last-name" type="text" id="last-name" value="<?php if (isset($lastName)) {echo htmlspecialcharacters($lastName);} ?>"/>
            <label for="email">E-Mail Address</label>
            <input name="email" type="text" id="email" value="<?php if (isset($email)) {echo htmlspecialcharacters($email);} ?>"/>
            <textarea name="message" id="message" ><?php if (isset($message)) {echo $message;} ?></textarea>
            <input type="submit" value="Send" class="button">
        </form>
        </section>
<?php } ?>

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

Any Help would be appreciated, thanks! Devin

1 Answer

Hey Devin,

I think I found your problem. You have the hidden username input field on your contact form, and you set its value to "username" when there should be no value set because that's what your if statement checks for.

<input name="username" type="hidden" value="username" />

To work with your if statement, it should read:

<input name="username" type="hidden" value="" />

Thank you so much Colin Marshall that worked perfectly! That was bothering me so much i couldn't think about anything else you've saved me man! And please if you have any other tips and tricks to tell me let me know. :)