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 not validating or submitting.

I'm trying to do the contact.php page in the build a simple php application but it's not validating or submitting. I pasted all the code below. Can anyone spot an error?

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

        if ($name == "" || $email == "" || $message == "") {
            echo "You must specifiy a value for name, email and message.";
            exit;
        }

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

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

        require_once("includes/class.phpmailer.php");
        $mail =  new PHPMailer();
        if (!$mail ->ValidateAddress($email)) {
            echo "You must specifiy a valid email address"; 
            exit;

        }

        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = "smtp.postmarkapp.com";
        $mail->Port = 26;
        $mail->Username = "#@#@#@#@-####-@@@@-#####-@#@#@#@#@#@#";
        $mail->Password = "#@#@#@#@-####-@@@@-#####-@#@#@#@#@#@#";

        $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 = "acha5066@gmail.com";
        $mail->addAddress($address, "Shirts 4 Mike");
        $mail->Subject = "Shirts 4 Mike Contact Form Submission | " . $name;
        $mail->MsgHTML($email_body);

if($mail->send()) {
    echo "Message Sent!"; 
} else {
    echo 'There was an error sending the mail' . $mail->ErrorInfo;
}

    header("Location: contact.php?status=thanks");
    exit;
}
?> 


<?php 

$pageTitle = "Contact Mike";
$section = "contact";

include('includes/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 will be in touch shortly! </p>

            <?php } else { ?>

        <p>I&rsquo;d love to hear from you. Complete the form to send me an e-mail.</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">Email</label>

                    </th>

                    <td>
                        <input type="text" name="address" id="address">
                        <p>Human's please leave this blank </p>
                    </td>
                </tr>





            </table>

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

        </form>

        <?php } ?>




    </div>
</div>

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

There should be a <?php tag at the top there but it's being cut off.

2 Answers

Got you error, there should be an undersorce "_" between REQUEST and METHOD. Change your second line from:

 if ($_SERVER["REQUEST METHOD"] == "POST") {

to this:

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

Thanks so much for your help. All working now :)

Actually, it is showing the thank you message now but there are no error messages showing when I don't input any data or when I fulfill the criteria of the other error conditionals.

I could be wrong but I think that the most recent version of PHPMailer depreciated ValidateAddress() function. Instead replace this line:

 if (!$mail ->ValidateAddress($email)) {

with this line:

  if(!filter_var($email, FILTER_VALIDATE_EMAIL)){

Check to see if your php errors are turned off, if they are, turn them on while you debug. Hope that helps!

Thanks for getting back to me. Now getting the error messages and I've updated my code and included the original PHPMailer file that Randy uses in the vid (not the latest release). I'm getting this notice message:

Notice: Undefined index: REQUEST METHOD in /Applications/MAMP/htdocs/contact.php on line 2


   <?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 specifiy a value for name, email 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("includes/class.phpmailer.php");
    $mail =  new PHPMailer();
    if (!$mail ->ValidateAddress($email)) {
        $error_message = "You must specifiy a valid email address"; 

    }

    if (!isset($error_messsage)) {
            $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_message = 'There was an error sending the mail' . $mail->ErrorInfo;
            }


    }
}

if (isset($error_message)) {
echo $error_message;
}
?><?php 
$pageTitle = "Contact Mike";
$section = "contact";
include('includes/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 will be in touch shortly! </p>

        <?php } else { ?>

    <p>I&rsquo;d love to hear from you. Complete the form to send me an e-mail.</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">Email</label
                </th>

                <td>
                    <input type="text" name="address" id="address">
                    <p>Human's please leave this blank </p>
                </td>
            </tr>





        </table>

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

    </form>

    <?php } ?>




    </div>
</div>

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

Obviously not a php issue here, but make sure to close your address label tag.

Thanks for the spot.

Dude, you are amazing, THAT was the thing I was looking for.

The PHP tutorial seems not up to date with phpmailer!