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 = "[email protected]";
$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" );
}
 ?>
3
  • reformat your html.... that is terrible formatting you got! Commented Mar 21, 2013 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. Commented Mar 21, 2013 at 16:46
  • echo include(... ? that's strange. see stackoverflow.com/questions/921479/echo-include-in-php Commented Mar 21, 2013 at 16:48

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you , I knew it, it must be something silly, but I was looking in PHP all the time. Works Great! Thanks Man
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.

Comments

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.

Comments

1

You forgot to add

name="name"

inside

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.