Problems with the return of $_POST values

Asked

Viewed 61 times

1

I am sending, through a form, an email. I do the tests, but it receives only 3 of the 4 fields, it is simply as if the field with the name and con_phone did not exist.

HTML:

<form class="contact-form top_60" method="POST" action="mail.php">
  <div class="row">
    <!--Name-->
    <div class="col-md-6">
      <input id="con_name" name="con_name" class="form-inp requie" type="text" placeholder="Nome">
    </div>
    <!--Email-->
    <div class="col-md-6">
      <input id="con_email" name="con_email" class="form-inp requie" type="text" placeholder="Email">
    </div>
    <!--Telefone-->
    <div class="col-md-6">
      <input id="con_telefone" name="con_telefone" class="form-inp requie" type="text" placeholder="Telefone">
    </div>
    <div class="col-md-12">
      <!--Message-->
      <textarea name="con_message" id="con_message" class="requie" placeholder="Como podemos ajudá-lo?" rows="8"></textarea>
      <button id="con_submit" class="sitebtn top_30" type="submit">Enviar</button>
    </div>
  </div>
</form>

PHP:

<?php
//require 'PHPMailerAutoload.php';
require 'phpmailer/class.phpmailer.php';
$mail = new PHPMailer;

$con_name = $_POST['con_name'];
$con_email = $_POST['con_email'];
$con_telefone = $_POST['con_telefone'];
$con_message = $_POST['con_message'];

$mail->setFrom($con_email, $con_name);
//$mail->addAddress('[email protected]', 'Oficina da Criação');
$mail->addAddress('[email protected]', 'Oficina da Criação');
$mail->Subject  = 'E-mail enviado pelo contato do site.';
$mail->Body     = '

    Nome: ' . $con_name . '
    Email: ' . $con_email . '
    Telefone: ' . $con_telefone . '
    Mensagem:
    ' . $con_message . '
    Post: ' . json_encode($_POST) . '
    Mensagem enviada no dia ' . date("d/m/Y") . ' às ' . date("G:i") . '
    IP do usuário: ' . $_SERVER["REMOTE_ADDR"];

if(!$mail->send()) {
  echo 'Message was not sent.';
  echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
?>

In the email I received this way:

Nome: Jakson Juliano Fischer
    Email: [email protected]
    Telefone: 
    Mensagem:
    a1s2d1a2s1d
    Post: {"con_name":"Jakson Juliano Fischer","con_email":"[email protected]","con_message":"a1s2d1a2s1d"}
    Mensagem enviada no dia 10/05/2019 às 13:56
    IP do usuário: 000.002.125.75

Someone’s been through this trouble and can help me?

  • 1

    Hi, modifies class="form-Inp requie" to class="form-Inp require" and in the part of the code that receives the POST method modifies $con_name = $_POST['con_name']; to $con_phone= Trim(addslashes($_POST['con_phone']);, does this for others, and tries to send, may be because you are not treating the variables that receive the POST method, and sending straight, is breaking.

  • 1

    See what returns giving a var_dump: var_dump($_POST);

  • Unfortunately, I tried to do it the way @Eliseub did. menciou, but it didn’t work, in relation to the var_dump cited by Victor, results in the same thing that json_decode ($POST) returned... It’s like there’s no field with the name com_phone :(

  • 1

    Ola @Jaksonfischer, Do not change the question title to indicate that your problem has been solved. If you have found a different response from the proposals by the community consider answering your own question, but later you may accept it, it may help people with the same problem =D -- I can answer my own question? -- If any community response helped you consider accepting, this would be the best way to thank. = D -- [Tour]

  • 1

    @Icaromartins thanks for the touch, I will make the necessary changes to meet the rules :D

2 answers

2


thanks for who volunteered and tried to help, but I found the mistake... Inside the template has a js responsible for the inputs, it clears all values sent to Php by $_POST if it is not declared, so that every time it identifies an additional value it deletes it.

I edited the document and added the new phone and it is sending normally.

If later someone else has this problem with templates, look in the JS folder and see if you don’t have a document called main.js, probably the whole magic happens inside it :D

Thank you very much for your time :D

1

Concatenate the content of the email in a more readable way.

$mail->Body = nl2br("Nome: $con_name\n"
    ."Email: $con_email\n"
    ."Telefone: $con_telefone\n"
    ."Mensagem: $con_message\n"
    ."Post: " . json_encode($_POST). "\n"
    ."Mensagem enviada no dia " . date("d/m/Y") . " às " . date("G:i") . "\n"
    ."IP do usuário: " . $_SERVER["REMOTE_ADDR"] . "\n");
  • I’m using Php Mailer, it does not accept tags like n, <br>, <Strong>, among others :/

  • Give a print_r on your $_POST, it does not receive the value filled in con_phone ?

  • I managed to solve, it was a damn JS file chipping everything kkkkkk Thank you so much anyway for the time you took to try to help :D

  • 1

    use btoa() to pass data from one side to the other in javascript helps a lot, so avoid breaking your string and executing the code with some characters, and on the other side decodes the Base64 which btoa converts. Good luck!

Browser other questions tagged

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