Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!