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

MUHAMMAD KHAN
MUHAMMAD KHAN
1,508 Points

Index Error

I am facing this error and i do not know how to fix this issue.

Notice: Undefined index: name in /Applications/MAMP/htdocs/salman/contact-process.php on line 3

Notice: Undefined index: email in /Applications/MAMP/htdocs/salman/contact-process.php on line 4

Notice: Undefined index: message in /Applications/MAMP/htdocs/salman/contact-process.php on line 5

Hi Muhammad,

Please post your code and what you trying to do :)

4 Answers

MUHAMMAD KHAN
MUHAMMAD KHAN
1,508 Points
<?php

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


?>
<?php

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

?>

The problem is that your assigning these variables to $_POST values, which will not be available if the form isn't been submitted.

To fix this you need to wrap your variables with if statement that check if the form was submitted, for example:

<?php

if( isset($_POST['THE_NAME_OF_YOUR_SUBMIT_BUTTON']) ) {

$name = $_POST["name"]; 
$email = $_POST["email"]; 
$message = $_POST["message"]; 
$email_body = ""; $email_body = $email_body . "Name: " . $name . "\n"; 
$email_body = $email_body . "Email: " . $email . "\n"; 
$email_body = $email_body . "Message: " . $message;
}
Hugo Paz
Hugo Paz
15,622 Points

Hi Muhammad,

I edited your post so the code is displayed better.

Are you trying to access the file directly through your browser?

If you are, the reason you are getting those errors is because those variables are not set.

You set them usually through a form. I will give you an example:

<form action="formHandler.php" method="post">
<input type="text" name="yourName" placeholder="name">
<input type="submit" value="Send">
</form>

On the formHandler.php file you can have something like this:

$name = $_POST["yourName"];

echo $name;

This will display whatever name inputted on the form. When you submit the form, the variable

$_POST["yourName"]

is set with the value typed and is accessible to the formHandler.php file to be used.

This is an extremely simplified example but I hope it will help you to understand.

MUHAMMAD KHAN
MUHAMMAD KHAN
1,508 Points

Thanks both of you Champions! let me check it again and if need help, will get back to you guys :)