Form receiving email with blank data

Asked

Viewed 389 times

0

The deal is that I created a form a few days ago and it worked fine, but I went to test it today and it receives the blank data.

Could you help me, please?

<?php var_export($_POST); exit; 
    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];

    $email_from = $_POST['email'];

    $email_subject = "New Form Submission";

    $email_body = "Nome: $name.\n".
                    "Email: $visitor_email.\n".
                        "Menssagem: $message.\n";


    $to = "[email protected]";

    $headers = "From: $email_from \r\n";

    $headers .= "Reply-To: $visitor_email \r\n";

    mail($to,$email_subject,$email_body,$headers);

    header("Location: contato.php")             

?>
<div class="contact-form">
    <form id="contact-form" method="post" action="contact-form-handler.php">
    <input name="name" type="text" class="form-control" placeholder="Seu nome" required> <br>
    <input name="email" type="email" class="form-control" placeholder="Seu email" required><br>
    <textarea name="message" class="form-control" placeholder="Mensagem" row="4" required></textarea>

    <input type="submit" class="form-control submit" value="Enviar mensagem">

    </form>

  </div>
  • Then the email arrives, but without content?

  • That, the ''name, email and 'message' fields all come blank, only with a dot . at the end

  • Put a var_export($_POST); exit; in the first line of contact-form-handler.php, fill in the form and tell us what appeared on the screen. You can [Edit] the question and add the result directly to it.

  • still remains the error and the page that appears now in the url is contact-form-Handler.php blank

  • Error? What mistake? There was no error in the story to keep appearing.

  • The error I say is that the email is received blank

  • But I shouldn’t have sent the e-mail; there is one exit there for the execution. Are you sure you put the code I quoted in the first line? Obviously it needs to be inside the PHP tags, not as you put it in the question.

  • I put the code you recommended before <?php, keep receiving the blank email and the following message appears now: var_export($_POST); Exit; Warning: Cannot Modify header information - headers already sent by (output Started at /home/eminente/public_html/contact-form-Handler.php:2) in /home/eminente/public_html/contact-form-Handler.php on line 24

  • Need to put inside the tag: <?php var_export($_POST); exit; ...

  • right, now it appears array ( ) and the email has not arrived

  • Which shows that your HTML is not sending the information because your $_POST is coming empty.

  • what should I do?

  • comment the Exit putting two bars in front of it and you will receive the email with the data. var_export($_POST); //exit;

Show 8 more comments

2 answers

0

Your PHP is incorrect.

  • First there is a exit right in the first line of PHP that serves to terminate the execution of the script.
  • If you comment or even remove this "function" from your script, an email will be sent, as you yourself qualified, "blank", in addition to being redirected to the page contato.php. So it doesn’t happen it is necessary to put a condition before executing the script.

The validation of the form no Back-end (PHP) is important because it avoids cases where the user enters the browser console, changes the form, such as removing the required, or the type email, and that way he can send the form the way you didn’t want it to be sent.

Follow a PHP validation email sending template

<?php

// Cria uma variável que terá os dados do erro
$erro = false;

// Verifica se o POST tem algum valor
if ( !isset( $_POST ) || empty( $_POST ) ) {
    $erro = 'Nada foi postado.';
}

// Cria as variáveis dinamicamente
foreach ( $_POST as $chave => $valor ) {
    // trim remove todas as tags HTML
    // strip_tags remove os espaços em branco do valor
    // $$chave cria as variaveis com os names dos elementos do formulário
    $$chave = trim( strip_tags( $valor ) );

    // Verifica se tem algum valor nulo
    if ( empty ( $valor ) ) {
        $erro = 'Existem campos em branco.';
    }
}


// Verifica se $email realmente existe e se é um email. 
if ( ( ! isset( $email ) || ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) && !$erro ) {
    $erro = 'Envie um email válido.';
}

// Se existir algum erro, mostra o erro
if ( $erro ) {
    echo $erro;
} else {

    // enviar um email
    $email_subject = "New Form Submission";

    $email_body = "Nome: $name.\n".
                    "Email: $email.\n".
                        "Menssagem: $message.\n";

    $to = "[email protected]";

    $headers = "From: $email \r\n";

    $headers .= "Reply-To: $email \r\n";

    mail($to,$email_subject,$email_body,$headers);

    header("Location: contato.php"); 

    /**** se não quiser redirecionar comente a linha acima e ***/
    /****      descomente o trecho de cpodigo abaixo        ***/

    /*
       echo "<h1>Dados enviados</h1>";

       foreach ( $_POST as $chave => $valor ) {
           echo '<b>' . $chave . '</b>: ' . $valor . '<br><br>';
       }

     */

       //limpamos as variaveis do formulario
       $name="";
       $email="";
       $message="";

}

?>


<div class="contact-form">
  <form id="contact-form" method="post" action="">
    <input name="name" type="text" class="form-control" placeholder="Seu nome" value="<?php echo $name ?>" required> <br>
    <input name="email" type="email" class="form-control" placeholder="Seu email"  value="<?php echo $email ?>" required><br>
    <textarea name="message" class="form-control" placeholder="Mensagem" row="4" required><?php echo $message ?></textarea>

    <input type="submit" class="form-control submit" value="Enviar mensagem">

  </form>

The above PHP validation is simple and works perfectly, but there are more advanced and effective validations for form validation.

0

My tip is that you check if your variables are being sent.

Use var_dump($to); and see the result. If null, are not receiving.

Browser other questions tagged

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