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

Notice: Undefined index: address in C:\xampp\htdocs\contact.php on line 21

I keep getting this message can anyone help me out if possible please? I don't notice anything wrong with my line.

<?php

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 address, 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("inc/phpmailer/class.phpmailer.php");           
    $mail = new PHPMailer();

    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;

    //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'll be in touch shortly.</p>
<?php } else {?>

    <p>I'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="address" 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'); ?>

2 Answers

In the form you have <tr style="display: none;"> for the address field. Probably this cause that when you submit the form the address field is not considered, so when you try to get his value in the if statement, you get the error.

Try to use isset to evaluate the field before get data. Something like:

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

Amit Sarker
Amit Sarker
599 Points

Thank you. It works.