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 Using A Third-Party Library

Email field not passing

Can someone please help me with this? I'm scratching my head on this. For some reason, the email field is not passing info. I confirmed this with a couple debug statements at the beginning. What am I missing?

Thanks so much.

<?php 

echo "Original email: " . $_POST["email"] . "<br>";

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

    if ($name == "" OR $email == "" OR $message == "") {
        echo "You must specify a value for name, email, and message." . "<br>";
        echo "name: " . $name . "<br>";
        echo "email: " . $email . "<br>";
        echo "message: " . $message . "<br>";
        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("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer(); // defaults to using php "mail()"
    if (!$mail->ValidateAddress($email)){
        echo "You must specify a valid email address.";
        exit;
    }

    $email_body = "";
    $email_body = $email_body . "Name: " . $name . "\n";
    $email_body = $email_body . "Email: " . $email . "\n";
    $email_body = $email_body . "Message: " . $message . "\n";

    // TODO: Send Email

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

}

?>
<?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 { ?>

            <p>I&rsquo;d love to hear from you! Complete the form to send me an email.</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="email" id="address">
                                <p>Don't fill this out.</p>
                            </td>
                        </tr>
                    </table>
                    <input type="submit" value="Send">

                </form>
            <?php } ?>
        </div>
    </div>

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

3 Answers

Carles Jove i Buxeda
Carles Jove i Buxeda
4,281 Points

The problem is in the form. You have to inputs named email, the later being the one that gets assigned to $_POST['email'].

<td>
     <!-- This is causing $_POST['email'] to be empty -->
     <input type="text" name="email" id="address">
     <p>Don't fill this out.</p>
</td> 

I guess you are trying to have an empty field as a simple method to filter spam. If you just set another name to this field, it'll work.

<td>
     <!-- Now $_POST['email'] has the user's email -->
     <input type="text" name="robot_catcher" id="address">
     <p>Don't fill this out.</p>
</td> 

EDIT: From the conversation with @jasonanello it turns out that you probably just have mistyped the last field, which should be:

<td>
     <!-- Now $_POST['email'] has the user's email -->
     <input type="text" name="address" id="address">
     <p>Don't fill this out.</p>
</td> 

Is the email coming back as an empty string?

Your address input also has a name of "email". I"m guessing that this "email" variable overwrites the one before? Since you have this field set to display:none; you're probably not entering anything in and maybe it's sending an empty string back.

Carles Jove i Buxeda
Carles Jove i Buxeda
4,281 Points

You are right noticing that two input fields have the name email. That is the error.

display:none doesn't interfere at all. CSS only affects layout, not the server side of things. In fact, hidden fields are a very common practice for filtering requests.

I probably should have been more clear but I did not think the css would prevent the input values from being sent.

I was only trying to suggest that since it wasn't displayed on the page it's likely he wasn't entering anything into that field when testing and so he's getting an empty email value back in the php script.

Now I see from your other answer that the point is not to have anything in there.

I skipped to the end of the video and it looks like the php script is expecting that name attribute to have the value "address"

Carles Jove i Buxeda
Carles Jove i Buxeda
4,281 Points

Oops. I didn't check the video, so I thought he was trying to get and empty field on purpose, when maybe it's all just a spelling mistake. Anyway, this dude has two correct answers now :-)

I haven't made it that for into the project but I think you're correct that it is supposed to be empty on purpose. I saw towards the end of the video that the php script is checking if it's empty and echoing an error message if it is not empty. The only problem then is that it's expecting the name to be "address".

Just noticed you edited your post to show this. We don't seem to receive notifications when posts are edited.

You have the more correct answer then :) You knew why this was being done.

Thanks very much for your help, Jason Anello , Carles Jove i Buxeda