PHP - Form with validation of fields sent over and over

Asked

Viewed 420 times

0

Following the suggestion of the moderator Sergio, I am sending a new question, since the previous one was flagged as duplicate and the only answer received did not solve the problem. But I want to make it clear that all suggestions given in answers to questions (Form inserting twice in the database and How to avoid sending PHP requests in a row) have been tested and have not solved my problem.

The suggestions given did not work because my form validates fields and with the suggested implementations, various ways to disable the Submit button, my form stopped validating the fields.

Problem: I have a PHP form that sends by e-mail the data provided by the user via Post method. In this form I validate some fields (CPF and some fields are required). When the user clicks on "Send" the process is sometimes slow and, some users, click several times on "Send" causing sending of the same form several times.

<?php
require_once '_js/ValidaCPF.php';

$todos_campos_preenchidos = TRUE;
$msg_email_enviado = FALSE;
$ValidaCPF = TRUE;

if (isset ($_POST['salvar'])) {

$contato = $_POST['contato'];
$email = $_POST['email'];
$cpf = $_POST['cpf'];

if(trim($_POST['contato']) == '') {
    $todos_campos_preenchidos = FALSE;
}elseif (trim($_POST['email']) == '') {
    $todos_campos_preenchidos = FALSE;
}elseif (trim($_POST['cpf']) == '') {
    $todos_campos_preenchidos = FALSE;
} 

$ValidaCPF = ValidaCPF($cpf);

If (($todos_campos_preenchidos) && ($ValidaCPF)) {

$para = "[email protected]";
$mailoculto = "[email protected]";

    //Codificações para envio do e-mail
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= "Reply-To: $email". "\r\n";
$headers .= "Bcc: $mailoculto";

    //Assunto do e-mail
    $assunto = "Contato";

    //Corpo da mensagem
    $mensagem = "<table><tr><td width='180'><strong>Assunto:</strong></td><td>Envio de contato</td></tr>";
$mensagem .= "<tr><td><strong>Contato: </strong></td><td>".$contato."</td></tr>";
    $mensagem .= "<tr><td><strong>E-mail: </strong></td><td>".$email."</td></tr>";
    $mensagem .= "<tr><td><strong>CPF: </strong></td><td>".$cpf."</td></tr>";
    $mensagem .= "<tr><td valign='top'><strong>Data e hora: </strong></td><td>" . date('d/m/Y - H:i') . "</td></tr>";
    $mensagem .= "<tr><td valign='top'><strong>IP's: </strong></td><td>" . get_ips() . "</td></tr></table>";


    //Envio do e-mail
    If (mail($para, $assunto, $mensagem, $headers)){

        //Enviando e-mail de confirmação para o e-mail cadastrado
        $assunto = "Confirmação de Contato";
        $mensagem_confirmacao = "Prezado(a) " . trim($contato) . ", <br><br>";
        $mensagem_confirmacao .= "Você está recebendo este e-mail por ter solicita cadastro. <br><br>";
        $mensagem_confirmacao .= $mensagem;
        $headers_confirmacao  = 'MIME-Version: 1.0' . "\r\n";
        $headers_confirmacao .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
        $headers_confirmacao .= "Reply-To: [email protected]". "\r\n";

        mail(trim($email), $assunto, $mensagem_confirmacao, $headers_confirmacao);            

        $contato = NULL;
        $email = NULL;
    $cpf = NULL;
        $msg_email_enviado = TRUE;
    }
}

}

Form call:

<form method="POST" name="solicitacao" action="">

CPF do empregado:<sup>*</sup></font></td>
<td valign="top"><input type="text" name="cpf" class="mask-cpf" size="29"
<?php if ($_SERVER['REQUEST_METHOD'] == "POST") { echo "value=\"" . $cpf . "\""; }?>></td>

<input type="submit" value="Enviar" name="salvar">

2 answers

1

For those who want to maintain the value validation of the Submit button and are using jquery there is the following solution.

var formEnviado = false;

$("#form").on("submit", function() {
    if(!formEnviado){
        formEnviado = true;
        return true;
    }else{
        return false;
    }
});

0

The simplest way, without messing with the code too much would be to just control whether or not it has already been submitted...

$('input[type=submit]').on('click',function() {
    $(this).attr('disabled','disabled');
});

I made it in jquery, but you can turn it into pure javascript..

  • 1

    I did it this way and it didn’t work because you lost the validation. <input type="Submit" value="Send" id="btnEnviar" onclick="javascript: this.form.Submit(); Document.getElementById(this.id). disabled = true;">

Browser other questions tagged

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