I have a form that registers the name of the person in a database via ajax, however sometimes it takes time to send and returns and I give 2 or 3 clicks it sends the same data! I wanted to know how to prevent this, I tried:
<script>
var statSend = false;
function checkSubmit() {
if (!statSend) {
statSend = true;
return true;
} else {
alert("O formulário esta sendo enviado…");
return false; }
}
</script>
and
onclick="this.disabled=true; this.value = 'Aguarde…'; this.form.submit();"
and among others, but none of them worked! someone help!?
<h4>Cadastro de Nome</h4>
<form id="cadUsuario" method="post">
<label>Nome:</label><input required type="text" name="nome" id="nome" />
<br/><br/>
<input type="submit" value="Salvar" id="salvar" />
</form>
<script type="text/javascript">
$(function () {
$("#cadUsuario").submit(function (e) {
e.preventDefault();
var FormData = $(this).serialize();
$.ajax({
type: "POST",
url: "salvar.php",
dataType: "json", // Add datatype
data: FormData
}).done(function (data) {
$('#cadUsuario').trigger("reset");
var mensagem = '';
if (!data.insert){
mensagem = 'Falha no cadastro';
}
/* if(!data.email){
mensagem += 'Falha no envio do email';
}*/
if(data.insert/* && data.email*/){
mensagem = 'Operação realizda com sucesso';
}
alert(mensagem);
}, "json");
return false;
});
});
</script>