-1

I'm trying to send an email with PHP using an HTML form.

When I receive the email however, the name and email fields are always empty, I only receive the message.

Here's the Form code:

<form class="form" id="contact" action="form-to-email.php" method="post">
<input class="name" type="text" id="name" placeholder="Name" required>
<input class="email" type="email" id="email" placeholder="Email" required>
<input class="phone" type="text" id="phone" placeholder="Phone No:">
<textarea class="message" name="message" id="message" cols="30" rows="10"     placeholder="Message" required></textarea>
<input class="submit-btn" id="submitbtn" type="submit" value="SUBMIT">
</form>

And PHP Code:

$myemail = 'example@gmail.com';//myemail
$name =filter_input(INPUT_POST, 'name');
$email_address = filter_input(INPUT_POST, 'email');
$message = filter_input(INPUT_POST, 'message');



$to = $myemail;

$email_subject = "Contact form submission: $name";

$email_body = "You have received a new message. ".

" Here are the details:\n Name: $name \n ".

"Email: $email_address\n Message: \n $message";

$headers = "From: $myemail\n";

$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
Tiago
  • 4,233
  • 13
  • 46
  • 70

2 Answers2

3

<input class="name"... is wrong, it should be <input name="WHATEVER"

The name property on the input is what you reference the form value by.

mikeb
  • 10,578
  • 7
  • 62
  • 120
1

You need to add name attributes to your input tags, as such:

<input class="name" name="name" type="text" id="name" placeholder="Name" required>
<input class="email" name="email" type="email" id="email" placeholder="Email" required>

You can read more here: http://www.html-form-guide.com/contact-form/php-email-contact-form.html

Tiago
  • 4,233
  • 13
  • 46
  • 70