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 Adding a Contact Form Working with Concatenation and Whitespace

balraj sarai
balraj sarai
6,303 Points

unidentified index

This error message keeps popping up when I run the submit button in my contact form.

Notice: Undefined index: name in C:\xampp\htdocs\contact-process.php on line 3

Notice: Undefined index: email in C:\xampp\htdocs\contact-process.php on line 4

Notice: Undefined index: message in C:\xampp\htdocs\contact-process.php on line 5 Name: Email: Message:

my current php code is..

<?php 

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"]; 
echo "Name: " . $name;
echo "Email: " . $email;
echo "Message: " . $message;

?>

I have linked the" contact.php" form to my "contact-process.php" file correctly. which is.. .

<form metohd="post" action="contact-process.php">

Someone please help.

3 Answers

Andrew Shook
Andrew Shook
31,709 Points

Balraj, the problem here is that $_POST['name'], $_POST['email'], and $_POST['message'] doesn't exist in the $_POST array and that is what PHP is trying to tell you. I assume you are trying to submit form data. So my guess is that the input fields' "name" attribute isn't actually set to "name", "email", or "message". On a side note, whenever you are using User data, always make sure that your validate and sanitize it.

Andrew Shook
Andrew Shook
31,709 Points

try replacing your form with this and see if it works:

<form method="post" action="contact-process.php">
    <label for="name">Name</label>
    <input type="text" name="name" id="name">
    <label for="name">Email</label>
    <input type="email" name="email" id="email">
    <label for="message">Message</label>
    <textarea name="message" id="message">
    </textarea>
</form>
Aaron Graham
Aaron Graham
18,033 Points

Andrew Shook beat me to it. Also, you might want to make sure the values actually exist. For example:

if (isset($_POST['name'])) {
  $name = $_POST['name'];
}

This will keep you from getting undefined index errors.

Andrew Shook
Andrew Shook
31,709 Points

Yeah I mentioned validating but got to lazy to write it out. To add to your's though, the variables should be set to empty strings first and then have the isset checks run. That way if the $_POST variable is empty no errors will occur.

Aaron Graham
Aaron Graham
18,033 Points

Andrew Shook - 100% right. I would probably handle this situation like this:

isset($_POST['name']) ? $name = $_POST['name'] : $name = '';

I didn't want to confuse the situation any more than necessary. I guess I failed at that... :-)

Andrew Shook
Andrew Shook
31,709 Points

AHH... I love a good ternary operator in the morning!

balraj sarai
balraj sarai
6,303 Points

my form is the default one we got when opening the project fie

<form metohd="post" action="contact-process.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="name" id="name"></textarea> 
                    </td>
                </tr>
            </table>
            <input type="submit" value="send"> 

        </form>
Andrew Shook
Andrew Shook
31,709 Points

Well I see one problem, but I don't know the implications of it. Right now your Message field name attribute is the same as the name attribute for the Name field. Try the code I posted above, or change the textarea to this:

<textarea name="message" id="message"></textarea> 

Also all of your label tags are missing an equals sign in their for attribute. They should look like this:

<label for="message">Message</label>