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

Robbie Thomas
Robbie Thomas
31,093 Points

PHP Form Issues

Currently on the Concatenation challenge and doing well. However, my own form doesn't seem to be doing well. I'm getting this error when I submit:

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: Robbee email: message:

The code is the same as Mr. Hoyt puts it out, unless I'm mistaken:

<pre><?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; echo $email_body;

?></pre>

Not sure what's wrong.

2 Answers

Hey Robert,

The code in your case and in Joe's case are perfectly valid code blocks. The "undefined index" is just simply referring to the fact that your $_POST array is empty or values in the $_POST array that you're trying to reference aren't defined yet. I ran the following in a test.php file and everything was fine after post submission,

<form method="post">
    <input type="text" name="name">
    <input type="text" name="email">
    <input type="text" name="message">
    <button type="submit">Submit</button>
</form>
<?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; 


var_dump($_POST);

An easy way to avoid the error is to just wrap the php code in the following conditional,

if($_SERVER['REQUEST_METHOD'] == 'POST') {
    $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; 

    var_dump($_POST);
}

Hope this helps.

Joe Bruno
Joe Bruno
35,909 Points

Hi Robert,

Could you specify the challenge that you are seeking to pass by providing the link? Without seeing the video, I would think the code should look something like this:

 <?php

    $name = $_POST["name"]; 
    $email = $_POST["email"]; 
    $message = $_POST["message"]; 

    $email_body = ""; 
    $email_body .= "name: " . $name . "\n"; 
    $email_body .= "email: " . $email . "\n"; 
    $email_body .= "message: " . $message; 

    echo $email_body;

 ?>