Submit sending several times

Asked

Viewed 380 times

1

I have a form where I check if the checkboxes are selected before sending Submit, but when the return is false (there is no checkbox selected), after selecting the checkbox and clicking the Submit button again, the Form is sent more than 1 time. When the validation is returned True the first time, Submit is sent only once, that is, I have no problem.

NOTE: I cannot change the Submit button to button because I need validation of the required form fields that Submit does. O . click() also need to identify the button that was clicked.

Could someone help unravel this unknown? Or do otherwise with the same result.

This is the jquery.

        function validaFormaPagto(){
            var tipopag = $(".tipopag").find(":checked").length;
            if (tipopag == 0) {
                return false;
            }
        }

        //Evento quando clicar em botões da classe concluir
        $(".concluir").click(function(){
            var valorBtn    = $(this).val();
                $("#forminput").submit(function(e){
                    var data = $("#forminput").serialize();
                    var numped = $('#numped').val();
                    if (!validaFormaPagto() ) {
                        return false;
                    }
                    $.ajax({
                        type : 'POST',
                        url  : 'config/concluirPedido.php',
                        data : data, //botao,
                        dataType: 'json',
                        beforeSend: function()
                        {   
                            $("#concluir").attr('disabled','disabled');
                            $("#imprimir").attr('disabled','disabled');
                        },
                        success :  function(response){                      
                            if(response.codigo == '2'){ 
                                $('#concluir').removeAttr('disabled');
                                $('#imprimir').removeAttr('disabled');
                                $('#forminput').fadeOut("fast");
                                $('#limpar').fadeOut("fast");
                                $('#mensagem').css('display', 'block')
                                .removeClass()
                                .addClass(response.tipo)
                                .html('')
                                .html('<span>' + response.mensagem + '</span>');

                                //Esconde a mensagem após 6 segundos e recarrega/redireciona a página
                                $("#mensagem").delay(4000).fadeOut("fast",function(e){
                                    window.location.reload();
                                });
                                //Verifica se imprimi o Pedido
                                if (valorBtn == 'Confirmar + Imprimir') {
                                    window.open('relimpressaoPedido.php?termo='+numped);
                                }
                            }
                            else {
                                $('#mensagem').css('display', 'block')
                                .removeClass()
                                .addClass(response.tipo)
                                .html('')
                                .html('<span>' + response.mensagem + '</span>');
                                //Esconde a mensagem após 6 segundos
                                $("#mensagem").delay(6000).fadeOut("fast");
                            }
                        }
                    });
                    e.preventDefault();
                    return false;
                });                    
        });
  • You only assign the event submit form when the user presses the button .concluir? Is that correct? This button is like submit, as described in the question? Have you tried using preventDefault to the click of this button also?

  • Try changing the last one return for true, to update it every time you click

  • @Andersoncarloswoss This, I only assign the Submit when pressing the button because I need to know which button was pressed and then perform another function based on the button value. The button is of type Submit. I put the preventDefault in the click, but there it does not execute the Submit.

  • @Felipedeolindo I made the change but nothing changed.

  • Do both , I saw that you are using two , so when press it reset and when you finish the cycle reset again , the declarant as false and the two uses as true

  • @Felipedeolindo I didn’t understand very well what you meant. If I put True the Submit will continue running, no?

Show 1 more comment

1 answer

0

I believe it is because, after the return of the false, the code outside the keys continues to be executed, regardless of the return. You can try to put ajax inside Else. For example:

if (!validaFormaPagto() ) {
     return false;
} else {
     $.ajax...
}

Browser other questions tagged

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