Name
Name

Reputation: 529

jquery check if username already in database using ajax

Every time when someone types something in the inputfield, I use ajax to check if the username already exists. But this query is very slow. I uploaded the page and it need 2-5s for this query. I use this code here

$('#register_username').bind('input propertychange', function() {
    $.ajax({
        type: "POST",
        url: "check_username.php",
        data: "username="+value
        success: function(response){
           //...
        }
    }
}

In check_username.php is only a sql statement. Im using a free webspace at the moment (lima-city.de). Is that the problem maybe?

You can test it here, enter in the first inputfield Benutzername

Upvotes: 0

Views: 639

Answers (2)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Try to do on focusout like below, and check once:-

$(document).bind('focusout','#register_username', function() {
    $.ajax({
        type: "POST",
        url: "check_username.php",
        data: "username="+value
        success: function(response){
           //...
        }
    }
}

Note:- Not only this it will depend on your query code also, that how much efficient it is. that means it will be optimized to give result in minimum amount of time. May be some problem is there also.I am not sure about that

Upvotes: 1

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

You can bind the blur event. It will send the query only once you loose focus of the input field.

Upvotes: 1

Related Questions