Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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>
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

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>
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda