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 Basic PHP Website (2018) Adding a Basic Form Working with Concatenation and Whitespace

Adebayo Ojo
Adebayo Ojo
23,661 Points

Notice: Undefined index: name/email in C:\wamp64\www\phpsite\process.php on line 2/3

I'm getting undefined index on line 2 and line 3 when I browse to my process.php page.

My code:

<?php $name = $_POST["name"]; $email = $_POST["email"]; $details = $_POST["details"];

echo "<pre>"; $email_body = ""; $email_body.= "Name ".$name. "\n"; $email_body.= "Email ".$email. "\n"; $email_body.= "Details ".$details. "\n"; echo "</pre>"; ?>

2 Answers

Adebayo Ojo
Adebayo Ojo
23,661 Points

Apparently it's this platform that messed up my code, because I have it correctly on my IDE. My code has the underscore but when I paste it here, the underscore is removed somehow. So with the correct code following the teacher, I'm still getting the "Undefined" error.

Simon Coates
Simon Coates
28,694 Points

undefined index means it isn't finding the value for one of the keys. You can take a look at what is on $_POST using var_dump($_POST ). I'll demo the error.

<?php
$item = [    "category"=>"Books"     ];
 $aVariable = $item["aardvark"];

produces:

<br />
<b>Notice</b>:  Undefined index: aardvark in <b>[...][...]</b> on line <b>4</b><br />

So you can look at what is stored in $_POST to see if anything is missing, and then you can look at the name attribute on the various input elements to see if there are any mismatches. Either you have the wrong key, are using GET not POST, or one of your values isn't being sent.

Adebayo Ojo
Adebayo Ojo
23,661 Points

See my var_dump output below. I don't know why the first two array keys(name and email) are having an underscore in the front. The actual code does not have anything like that.

C:\wamp64\www\phpsite\process.php:11: array (size=3) 'name_' => string 'Ojo Adebayo' (length=11) 'email_' => string 'bayoojo1@yahoo.com' (length=18) 'details' => string 'message' (length=7)

Simon Coates
Simon Coates
28,694 Points

the name attribute on the input form elements should determine what key your values have inside $_POST.

Adebayo Ojo
Adebayo Ojo
23,661 Points

The was in my php.ini file. I had to edit the SMTP and smtp_port field with the same detail I have in my suggest.php code, and it's working perfectly now. I can send and receive email right from my local machine.