Empty fields Phpmailer

Asked

Viewed 105 times

0

I have a form from which when fired, the fields arrive empty in the email. Does anyone know why this is happening? See below:

FORM

<form method="post" id="form">
        <div class="form-group">
          <label for="nome">Nome:</label>
          <input type="text" name="Nome" class="form-control" id="nome" value="" style="width: 100%">
        </div>
        <div class="form-group">
          <label for="email">Email:</label>
          <input type="text" name="Email" class="form-control" id="email" value="" style="width: 100%">
        </div>
        <div class="form-group">
          <label for="mensagem">Mensagem:</label>
          <textarea name="Mensagem" class="form-control" id="mensagem" value="" style="height: 100px"></textarea>
        </div>
    <div align="center">
        <button type="submit" class="btn btn-primary"><i class="fa fa-envelope" aria-hidden="true"></i> Enviar</button>
    </div>
    </form>

JQUERY

<script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>                
<script type="text/javascript">
$(document).ready(function(){
   $('#form').submit(function() {
     data = $('#form').serialize();

      $.ajax({
    type: "POST",
    url: "validar.php",
    data: dados,
    success: function( data )
    {
     if(data == 1){
       $('#myModal').modal('show');
    }else{
       $('#form input').val("");
       $('#form textarea').val("");
       $('#myModal1').modal('show');           
    }
 }
      return false;
    });
});
</script>

VALIDATE.PHP

    $nome = $_POST["Nome"];
    $email = $_POST["Email"];
    $mensagem = $_POST["Mensagem"];

    require_once('phpmailer/PHPMailerAutoload.php');
    $mail = new PHPMailer();
    $mail->IsSMTP();
    $mail->Host = "mail.site.com.br"; 
    $mail->Port  = '587';
    $mail->Username = '[email protected]';
    $mail->Password = "senha";

    $mail->From = $email;
    $mail->FromName = $nome;
    $mail->AddAddress("[email protected]");
    $mail->IsHTML(true);
    $mail->Subject = "Mensagem do site Empresa"; 

    $data = date("d/m/Y");
    $hora = date("H:i");

    $mensagem = "Mensagem enviada no dia ".$data. " às " .$hora."<br><br>";
    $mensagem .= "<strong>Nome:</strong> ".$nome."<br>";
    $mensagem .= "<strong>Email:</strong> ".$email."<br>";
    $mensagem .= "<strong>Mensagem:</strong> ".$mensagem."<br>";

    $mail->Body = $mensagem;

    if(!$mail->Send()){
        $erro = 1;       
    }else{
        $erro = 0;
    }   
echo $erro;

RESULT

In addition to the fields arriving empty, duplicates are also arriving and there are no links in the archive validate.php:

Mensagem enviada no dia 04/08/2017 às 11:54

Nome: 
Email: 
Mensagem: Mensagem enviada no dia 04/08/2017 às 11:54

Nome: 
Email:
  • 1

    Take a look at the POST examples from Jquery

  • Hello Everson. I made a change to my post based on your information, but still the error persists.

  • 1

    In the AJAX call you need to use the date variable once you are setting it to receive the serialized form, correct? url: "validate.php", date: -> date, You entered data!

  • 1

    Debug your code to check if variables are being filled in when you receive the POST. If not, add the encoding in the upload with $mail->CharSet = 'UTF-8';

  • Thanks to all. I managed to solve. I changed the line as the miltoncamara reported.

1 answer

2


Change:

$nome = $_POST["Nome"];
$email = $_POST["Email"];
$mensagem = $_POST["Mensagem"];

For:

$nome = $_POST["nome"];
$email = $_POST["email"];
$mensagem = $_POST["mensagem"];

And let the name= equal to id=, everything in minusculo, for example change from this:

<input type="text" name="Nome" class="form-control" id="nome" value="" style="width: 100%">

For this:

<input type="text" name="nome" class="form-control" id="nome" value="" style="width: 100%">

Should stay like this:

    <div class="form-group">
      <label for="nome">Nome:</label>
      <input type="text" name="nome" class="form-control" id="nome" value="" style="width: 100%">
    </div>
    <div class="form-group">
      <label for="email">Email:</label>
      <input type="text" name="email" class="form-control" id="email" value="" style="width: 100%">
    </div>
    <div class="form-group">
      <label for="mensagem">Mensagem:</label>
      <textarea name="mensagem" class="form-control" id="mensagem" value="" style="height: 100px"></textarea>
    </div>

Browser other questions tagged

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