User Availability question

#1

Dear members, I have never taken time to learn AJAX. I know very little about it other than reading that Google made use of it many years ago for suggested searches and ai to finish your sentences. Very cool.

Anyway, i find myself wanting to check username availabilty in this manner, so i guess that i need to know AJAX. I don’t like jQuery so i’m looking for some references about this subject using JavaScript with AJAX to check user availability securely. Anyone know of a good tutorial? I’ve tried searching Google and i found a pdf file but it turns out to be an O’Reilly book hosted at a university site. I don’t think that this is allowed. So i am not able to find info specifically for this matter. Any suggestions?

#2

Ajax isn’t really anything magical. It’s simply using JS to perform HTTP requests to the web server which means you can submit data, request data, etc without reloading the web page.

In this example you could have a JS script listening to changes on the username field and on change (remember to wait a sec so you don’t fire requests for every letter they write) send a HTTP POST request to ie usernameAvailable.php. In this example the php file could simply either return 200 OK or 403 forbidden, “true” and “false”, or whatever else you find sensible in order to differentiate between available and not available usernames.

var data = "" // fetch the username from the HTML DOM

var request = new XMLHttpRequest();
request.open('POST', '/usernameAvailable.php', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // Success!
    var data = JSON.parse(request.responseText);
  } else {
    // We reached our target server, but it returned an error
  }
};

request.send(data);
1 Like
#3

Thank You, Jim. I feel like i should compensate you for all of the advice that you give to me and time that you spend answering my newbie questions. I really do appreciate you.