Following the suggestion of the moderator Sérgio, 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 response to doubts (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 does field validation and with the suggested implementations, several ways to disable the Send button, my form stopped validating the fields.
Problem: I have a PHP form that e-mails user-supplied data via the Post method. In this form I validate some fields (CPF and some fields are required). When the user clicks "Send" the process is sometimes slow and, some users, clicks several times on the "Send" causing the same form to be sent 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">