Prevent double submission of forms in Submit

Asked

Viewed 21 times

-1

How can I prevent the user from doing multiple submissions of a form? My code is like this:

// VALIDAÇÃO DO CADASTRO
$(document).ready(function(e){          
    $(function () {
        // VALIDAÇÃO
        $("#frmCadastro").validate({                        
            rules: {    
               IdUnicoop: {
                  required: true
               }
            },                      
            messages: {
               IdUnicoop: {
                  required: ""
               } 
            },
            // Do not change code below
            errorPlacement: function (error, element) {
                error.insertAfter(element.parent());
            },
            submitHandler: function (form) {
                var form = $("#frmCadastro");
                var formdata = false;
                
                if (window.FormData){
                      formdata = new FormData(form[0]);
                }
                
                $.ajax({
                    type: 'POST',
                    url: 'pAddEstagio.php',
                    data: formdata ? formdata : form.serialize(),
                    dataType: 'json',                                
                    cache: false,
                    contentType: false,
                    processData: false,                                
                    beforeSend: function () {
                        // $("#msgResult").html('AVISO! Enviando...');
                    },
                    success: function (response) {
                        e.preventDefault(); 
                        if (response.codigo == "1") {                                        
                            bootbox.alert({
                                message: response.mensagem,
                                callback: function () {
                                    window.location.href = "iAddEstagio.php";
                                }
                            })                                  
                        } else { 
                            e.preventDefault();                                        
                            bootbox.alert({
                                message: response.mensagem,
                                callback: function () {
                                    window.location.href = "iAddEstagio.php";
                                }
                            })                                                                                  
                        }                                
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        console.log(xhr, ajaxOptions, thrownError);
                        
                        bootbox.alert({
                            message: "ATENÇÃO! Ocorreu um erro ao tentar enviar a solicitação. Contate o suporte técnico.",
                        })                                     
                        
                         // $("#msgResult").html('ATENÇÃO! Ocorreu um erro ao tentar enviar a solicitação. Contate o suporte técnico.');
                    }
                });
                return false;
            }
        });
    });
}); 

I tried to use the preventDefault but without success.

  • 1

    Broad question. 1 - Disable Submit in Success. 2-make use of localStorage or sessionStorage ( will depend on what this double sending refers to whether during the session or not) to inhibit Submit.

1 answer

1

Try to put an attribute in the form onSubmit="return false" after you have made your validations remove the attribute onSubmit and call .submit() for your form as you suspended the automatic Submit will need to do manually.

Browser other questions tagged

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