Check User Name Availability On The Fly with AJAX
April 19th, 2008 | by valiik |A quick AJAX script that will check User Name availability on the fly.
Insert this JavaScript into the HEAD of your page, write a file called checkui.php or asp that will check availability of the username in your database and write the message to the screen, then this snipit of JavaScript will write that message to the SPAN on your page.
// AJAX USERNAME AVAILABILITY CHECK
// - uses PHP file: checkui.phpfunction xmlhttpPost(theui) {
var strURL = “/checkui.php?uid=”+theui; // URL OF THE PHP/ASP FILE
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject(”Microsoft.XMLHTTP”);
}
self.xmlHttpReq.open(’POST’, strURL, true);
self.xmlHttpReq.setRequestHeader(’Content-Type’, ‘application/x-www-form-urlencoded’);
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(getquerystring());
}// GRABS THE USERNAME FROM THE FORM AND ADDS IT TO THE PHP/ASP FILE URL
function getquerystring() {
var form = document.forms[’u_register’];
var word = form.m_username.value;
qstr = ‘uid=’ + escape(word); // NOTE: no ‘?’ before querystring
return qstr;
}// SENDS THE FINAL RESULTS TO THE SPAN ON THE PAGE
function updatepage(str){
document.getElementById(”checkui”).innerHTML = str;
}// END AJAX USERNAME AVAILABILITY CHECK
NOTE: This is a quick reference, not an extensive explanation of AJAX.





