Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8927133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:08:59+00:00 2026-06-15T08:08:59+00:00

I have a small jQuery/AJAX script that checks if a username is already in

  • 0

I have a small jQuery/AJAX script that checks if a username is already in use or not. It queries a PHP script, which in turn outputs the number of users with the specified username. I have an error counter that keeps track of the number of validation errors on the form, and I increment the counter if the username is already in use.

//Keep track of number of errors
 var errors = 0;
 //Keep track of error messages
 var errorMsg = "";

//Check if the passwords match (this part works as expected)
 if($("#password").val() != $("#repeat").val()) {
     errors++;
     errorMsg += "<li>Passwords do not match!</li>";
 }

     //Now check if the user exists
     var userName = $("#user").val();
    $.ajax({   
        type: "POST",
        data : {'user': userName},
        cache: false,  
        url: "js/ajax/addUser.php?action=checkuser",   
        success: function(data){
            var result = parseInt(data);
            //For debugging purposes
            alert("Before checking the data, # of errors is: " + errors);
            if(result > 0) {
                errors++;
                errorMsg += "<li>A user with the name <strong>" + userName + "</strong> already exists!</li>";
            }
            //For debugging purposes
            alert("After checking the data, # of errors is: " + errors);

        }
    });
    //For debugging purposes
    alert("Before validating the form, # of errors is: " + errors);

     if(errors > 0) {
         //Output error message
     }
     else {
         //Send the form
     }

As mentioned in the comments, I have a few alert()‘s for debugging. The first two (within the AJAX request) display the correct amount of errors. However, when I get to the third one, it completely disregards any errors that occurred within the AJAX request.

I have trimmed my code down to just the essentials, but if the error is not clear within the snippet provided, I can post the entire thing.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:09:00+00:00Added an answer on June 15, 2026 at 8:09 am

    ajax is asynchronous. Place your logic inside the success.

    //Keep track of number of errors
     var errors = 0;
     //Keep track of error messages
     var errorMsg = "";
    
    //Check if the passwords match (this part works as expected)
     if($("#password").val() != $("#repeat").val()) {
         errors++;
         errorMsg += "<li>Passwords do not match!</li>";
     }
    
         //Now check if the user exists
         var userName = $("#user").val();
        $.ajax({   
            type: "POST",
            data : {'user': userName},
            cache: false,  
            url: "js/ajax/addUser.php?action=checkuser",   
            success: function(data){
                var result = parseInt(data);
                //For debugging purposes
                alert("Before checking the data, # of errors is: " + errors);
                if(result > 0) {
                    errors++;
                    errorMsg += "<li>A user with the name <strong>" + userName + "</strong> already exists!</li>";
                }
                //For debugging purposes
                alert("After checking the data, # of errors is: " + errors);
    
                if(errors > 0) {
                    //Output error message
                }
                else {
                    //Send the form
                }
            }
        });
        //For debugging purposes
        alert("Before validating the form, # of errors is: " + errors);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small jQuery plugin that I use for form AJAX validation. There
I have a small ajax jquery script which returns some XML for me. Whilst
i have a small jquery script that when I click an ajax actionlink in
I have this small jquery script that does not work if I remove the
I'm new to JQuery and have a script that creates a dialog via Ajax
I have a bit of jquery that makes an ajax request to a script
I have a small jQuery script that gets information by looking at an ID.
So I have a script which checks the login details.. I have that working,
I have a small jquery snippet that displays notification message at the top of
I have a small form that contains a jQuery UI dialog to ask for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.