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

119
Visualizações
How to check ajax/php response async?

this code doesn't work, because it always return undefined. In my opinion the "success" from the ajax should return, if the result is there.

How to make sure, that the boolean will be returned?

<script>
        $('#sbtn').on("click", function(e) {
                e.preventDefault();
                
                // if user exists, set validated to true
                var validated = checkUser('requestedUser');
                
                
                
                // if the user is validated, submit form
                if(validated) {
                    //alert("Thank You");
                    $('#setup').submit();
                }
        }
        
        function checkUser(user) {
                
                data = {
                    lernsax_email: user
                }
                
                $.ajax({
                  type: "POST",
                  url: "checkuser.php",
                  data: data,
                  success: function(msg){
                        if(msg === "passed") {
                            // php returns "passed", if the user can be found
                            console.log(msg);
                            return true;
                            
                        }   else {
                            console.log(msg);
                            return false;
                        }               
                        
                    },
                  
                });
        }
    
    
</script>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

you can use Promise

<script>
    $('#sbtn').on("click", function(e) {
            e.preventDefault();
            
            // if user exists, set validated to true
            let result = checkUser('requestedUser');
            result.then(validated => {
                if(validated) {
                    //alert("Thank You");
                    $('#setup').submit();
                }
            });
    }
    
    function checkUser(user) {
            data = {
                lernsax_email: user
            }
            return new Promise(resolve => {
                $.ajax({
                  type: "POST",
                  url: "checkuser.php",
                  data: data,
                  success: function(msg){
                        if(msg === "passed") {
                            // php returns "passed", if the user can be found
                            console.log(msg);
                            resolve(true);
                            
                        }   else {
                            console.log(msg);
                            resolve(false);
                        }               
                        
                    },
                  
                });
            });
    }
</script>

or move

if(validated) {
   //alert("Thank You");
   $('#setup').submit();
}

to success funtion of ajax

<script>
    $('#sbtn').on("click", function(e) {
            e.preventDefault();
            
            // if user exists, set validated to true
            checkUser('requestedUser');
    }
    
    function checkUser(user) {
            
            data = {
                lernsax_email: user
            }
            
            $.ajax({
              type: "POST",
              url: "checkuser.php",
              data: data,
              success: function(msg){
                    if(msg === "passed") {
                        // php returns "passed", if the user can be found
                        console.log(msg);
                        $('#setup').submit();
                        return true;
                        
                    }   else {
                        console.log(msg);
                        return false;
                    }               
                    
                },
              
            });
    }
</script>
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