The email arrives, but with the fields that the user has filled in empty

1

good, I have this problem with my form at the time of putting send check my email and I get a message but with empty fields someone knows how to fix it? attached code

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))


$name = $_POST['name'];
$email_address= $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

// Create the email and send the message
$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

and my code of the form is as follows:

 <form name= "sentMessage" " method="post" action="contact_me.php">
 <div class="row">
 <div class="col-md-6">
 <div class="form-group">
 <input type="text" class="form-control" placeholder="Nombre *" id="name" required data-validation-required-message="Please enter your name.">
     <p class="help-block text-danger"></p>
      </div>
     <div class="form-group">
  <input type="email" class="form-control" placeholder="E-mail *" id="email" required data-validation-required-message="Please enter your email address.">
  <p class="help-block text-danger"></p>
      </div>
  <div class="form-group">
  <input type="tel" class="form-control" placeholder="Telefono *" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Mensaje *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
 <div class="clearfix"></div>
 <div class="col-lg-12 text-center">
 <div id="success"></div>
 <button type="submit" class="btn btn-primary">enviar mensaje</button>
 </div>
 </div>
 </form>
 </div>
 </div>

If you can help me, I would really appreciate it.

    
asked by Hector 12.11.2016 в 03:02
source

1 answer

1

To receive the values of your form you should put the attribute name

Example:

<input type="text" class="form-control" name="name" placeholder="Nombre *" id="name" required data-validation-required-message="Please enter your name.">
                                        ^^^^  
    ...

<input type="email" class="form-control" name="email" placeholder="E-mail *" id="email" required data-validation-required-message="Please enter your email address.">
                                         ^^^^
<!-- y así con todos -->

And then you check badly on condition if () {} :

You have to put ! in front of empty() so that you return true in case it is not empty and convert || in && so that all conditions are true , example:

if(!empty($_POST['name'])        &&
   !empty($_POST['email'])       &&
   !empty($_POST['phone'])       &&
   !empty($_POST['message'])     &&
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {

   // Enviar mail
}
    
answered by 12.11.2016 / 03:08
source