1

I created very simple message form and there is some problem. At least message is send and there is display thank you page, but there is Notice: Undefined index: name in public_www/n..../contact-form-handler.php on line 14 and another one error is that name is not send.

Thanks for any hint

<form class='contact_form' method="POST"  action="contact-form-handler.php" > 
<ul>
<li>
<label for="name"  >Name:</label>
<input type="text"  id="name" />
</li>
<li>
<label for="email" id="email">Email:</label>
<input type="email" name="email"  />
</li>
<li>
<label for="message" id="message">Message:</label>
<textarea name="message" cols="40" rows="6" required ></textarea>
</li>
<li>
<button class="submit" value="submit">Submit Form</button>
</li>
</ul>
</form>

and PHP is:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")  {
$to = "test@test.com";
$subject = "Contact";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$message = $_POST['message'];
$body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
echo   include( "contact-form-thank-you.html" );
    mail($to, $subject, $body);
} else {
echo include( "contact-form-error.html" );
}
 ?>
  • reformat your html.... that is terrible formatting you got! – ITroubs Mar 21 '13 at 16:46
  • Please do research on what `Undefined index` means. I would also recommend you use an existing library or class such as [PHPMailer](http://sourceforge.net/projects/phpmailer/). – Kermit Mar 21 '13 at 16:46
  • `echo include(...` ? that's strange. see http://stackoverflow.com/questions/921479/echo-include-in-php – Funk Forty Niner Mar 21 '13 at 16:48

4 Answers4

2

Yout html is wrong! this should work:

<form class='contact_form' method="POST"  action="contact-form-handler.php" > 
<ul>
<li>
<label for="name"  >Name:</label>
<input type="text"  name="name" />
</li>
<li>
<label for="email" id="email">Email:</label>
<input type="email" name="email"  />
</li>
<li>
<label for="message" id="message">Message:</label>
<textarea name="message" cols="40" rows="6" required ></textarea>
</li>
<li>
<button class="submit" value="submit">Submit Form</button>
</li>
</ul>
</form>

you mixed up id and name of your name input

ITroubs
  • 11,094
  • 4
  • 27
  • 25
  • Thank you , I knew it, it must be something silly, but I was looking in PHP all the time. Works Great! Thanks Man – user2195583 Mar 21 '13 at 16:53
2

when you want to pass something using a form the important attribute is "name":

<input type="text"  id="name" name="name" />

with this it should work.

The id is used to reference the element from the DOM, i.e when using Jquery or javascript.

The name is the one used when submiting a form.

Naryl
  • 1,878
  • 1
  • 10
  • 12
2

Your "name" input does not have a name attribute. The id attribute, by itself, does not mean a $_POST array entry gets sent to your script. Only the data in inputs with name attributes get sent to your PHP script, as long as they don't have a disabled attribute.

toon81
  • 868
  • 1
  • 5
  • 13
1

You forgot to add

name="name"

inside

<input type="text"  id="name" />
Nikitas
  • 1,013
  • 13
  • 27