Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
Form didn't submit after ajax validation

This question had been asked a lot of times, but I still confused where I made a mistake. I need to validate form by ajax and submit it if everything is ok. Here is my code:

    $('#tl_form form').on('submit',function(e) {
        var $form = $(this);
        if($form.attr('validated')){
                        //Here the form should be send IMHO, but "return true" don't do it somehow:
                        console.log('ok'); // I can just see this 'ok' in console.
            return true;
        }
        $.ajax({
            type: "POST",
            data: $form.serialize() + '&action=validate_tl_form',
            url: ajaxurl,
            success: function(data) {
                if (data.success) {
                    $form.attr('validated',true).submit();
                } else {
                                        console.log('error');
                }
            },
        });
        return false;
    });

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I would recommend to re-work your logic.

If you are submitting the entire form to the server anyway for validation, the server might as well simply accept it when it is valid, instead of sending "it is valid" to the client and expecting a form re-post.

You can show a validation hint on the client when the form was not valid.

$('#tl_form form').on('submit', function (e) {
    e.prenventDefault();
    $.post($(this).attr('action'), $(this).serialize()).done(function (data) {
        if (data.success) {
            // go to next step/page
        } else {
            // show validation hint
        }
    });
});

e.prenventDefault(); prevents that the browser submits the form the regular way, since you're sending it via Ajax.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda