Form receiving email with blank data

0

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

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>
    
asked by anonymous 08.06.2018 / 21:41

2 answers

0

Your PHP is incorrect.

  • First there is a exit soon in the first line of PHP that serves to terminate script execution.
  • If you comment or even remove this "function" from your script, you will be sent an email, as you called yourself, "blank", as well as being redirected to the contato.php page. To prevent this from happening you must put a condition before running the script.
  

Form validation in Back-end (PHP) is important because it avoids cases where the user enters the browser console, changes the form, such as removing required , or type email , and that way he can send the form the way you did not want it sent.

Follow an email template with PHP validation

<?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 PHP validation above is simple and works perfectly, however, there are more advanced and effective validations for form validation.

    
09.06.2018 / 10:56
0

My tip is to make sure your variables are being sent.

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

    
09.06.2018 / 02:31