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 trialbalraj sarai
6,303 Pointsunidentified 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
31,709 PointsBalraj, 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.
Aaron Graham
18,033 PointsAndrew 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
31,709 PointsYeah 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
18,033 PointsAndrew 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
31,709 PointsAHH... I love a good ternary operator in the morning!
balraj sarai
6,303 Pointsmy 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
31,709 PointsWell 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>
Andrew Shook
31,709 PointsAndrew Shook
31,709 Pointstry replacing your form with this and see if it works: