I'm trying to write something using jQuery's AJAX API that submits a username to the database and then does something with the response. My script works, sort of. Here is my jQuery code:
$(document).ready(function(){
$("#fname").bind('keyup blur focus load', function() {
$.ajax({
type: "get",
cache: true,
url: "ajaxphp/checkaccess.php",
dataType: "text", //dataType can be html, json, jsonp, script, or text.
data: {
fname: $('#fname').val()
},
before: function() {
//Before the data is sent we can call this method to preprocess,
//hide elements on the page, etc.
},
error: function (data) {
//If the request fails, run this error function.
},
success: function (data) {
//On a successful XmlHttpRequest call this function is called
$('#fnamecheck').html(data);
}
}); //end of the ajax call attached to the username onchange function
}); //end of the onchange function
});
The problem is that the server is a bit slow in parsing back the response. How this works is I type in a username and then I get back a response containing the username and either some text that says available or not available. I can see though that if I type a username, say MyUsername, sometimes the response I get will be like "MyUse is available", meaning that only part of the value was returned. If I wait long enough eventually it will say "MyUsername is available". What I want to do is if it is waiting for a response I want to show a loading message instead of an incorrect message. Also, is there a way to cancel all requests made via the script except the last one? I don't want the server to have to do 8 database lookups when the user types username in my form, but I want it where the second they are done typing what they put in the box is submitted to the server, without the intermediary steps. I looked at the change event, but that seems to only work if the form field loses focus.
Thanks,
BMR777

New Topic/Question
Reply



MultiQuote



|