PHP does not take phone field information

-1

I'm creating a form to send direct email from a site, via PHP . However, when I add the field to put the phone to contact the code PHP , it does not seem to recognize the field and does not get the information from that field. I made some tests in the code in PHP and if I put some other variable in the phone field the same appears in the email and if I use the telephone field in another variable the same does not to appear the information. >

PHP code:

<?php
// Check for empty fields
if(empty($_POST['phone'])       &&
    empty($_POST['name'])       ||
   empty($_POST['email'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "Campo preenchido incorretamente!";
    return false;
   }

$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 = "E-mail enviado por  $name";
$email_body = "E-mail de contato enviado por $name \n";

$email_body .= "\nNome: $name
\nTelefone: $phone
\nE-mail: $email_address 
\nMensagem:\n $message \n";



$headers = "From: $email_address\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;
?> 

HTML:

<form  name="sentMessage" id="contactForm" action="contact_me.php"  method="post"> 
        <!--name="sentMessage" id="contactForm" novalidate-->
    <div class="row">
      <div class="col-md-6">
        <div class="form-group">
          <input type="text" id="name" name="name" class="form-control" placeholder="Nome" required="required">
          <p class="help-block text-danger"></p>
        </div>
      </div>
      <div class="col-md-6">
        <div class="form-group">
          <input type="email" id="email" name="email" class="form-control" placeholder="E-mail" required="required">
          <p class="help-block text-danger"></p>
        </div>
      </div>

       <div class="col-md-6">
        <div class="form-group">
          <input type="text" id="phone" name="phone" class="form-control" placeholder="Telefone" required="required">
          <p class="help-block text-danger"></p>
        </div>
      </div>

    </div>
    <div class="form-group">
      <textarea name="message" id="message" name="message" class="form-control" rows="4" placeholder="Mensagem" required></textarea>
      <p class="help-block text-danger"></p>
    </div>
    <div id="success"></div>
    <button type="submit" class="btn btn-default">Enviar</button>
  </form>
    
asked by anonymous 20.01.2018 / 13:35

1 answer

1

Basically what was missing was the name attribute of the inputs.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<form action="contact_me.php" method="post">
  <!--name="sentMessage" id="contactForm" novalidate-->
  <div class="row">
    <div class="col-md-6">
      <div class="form-group">
        <input name="name" type="text" id="name" class="form-control" placeholder="Nome" required="required">
        <p class="help-block text-danger"></p>
      </div>
    </div>
    <div class="col-md-6">
      <div class="form-group">
        <input type="email" name="email" id="email" class="form-control" placeholder="E-mail" required="required">
        <p class="help-block text-danger"></p>
      </div>
    </div>

    <div class="col-md-6">
      <div class="form-group">
        <input type="tel" id="phone" name="phone" class="form-control" placeholder="Telefone">
        <p class="help-block text-danger"></p>
      </div>
    </div>
  </div>

  <div class="form-group">
    <textarea name="message" id="message" class="form-control" rows="4" placeholder="Mensagem" required></textarea>
    <p class="help-block text-danger"></p>
  </div>
  <div id="success"></div>
  <button type="submit" class="btn btn-default">Enviar</button>
</form>
    
20.01.2018 / 14:45