PHP does not take information from the phone field

Asked

Viewed 111 times

-1

I am creating a form to send e-mail direct from a website, through the PHP. However, when I add the field to put the phone to contact the code PHP, You don’t seem to recognize the field, and you don’t get the information from that field. I ran some tests on the code on PHP and if I put some other variable in the phone field the same appears in the email and if I use the phone field in another variable the same does not appear the information.

Code in PHP:

<?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>
  • missing the name of inputs in almost all!

1 answer

1


Basically what was missing was the attribute name of inputs.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="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>

  • even adding the "name" attribute in the fields still not receiving what is written in the phone field.

  • I did the test here with the same PHP code that you posted and when I print the string $email_body print everything right. using the above HTML code gave no problem

  • do the following, comment all the PHP code and just have this function run and paste here the return of it: var_dump($_POST)

  • Now it’s working, thank you very much Phelipe. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.