I’m trying to get a hold on AJAX and have been looking at the Google tutorial which describes a script to check if a username that you type in to a form is available.
I’ve patched together the code but nothing seems to happen. Here is what I’ve done:
[code=html]<!DOCTYPE html>
<html>
<head>
<meta content=”text/html; charset=utf-8″ http-equiv=”Content-Type”>
<title>Ajaxer</title>
<script type=”text/javascript”>
var obj;
function ProcessXML(url) {
// native object
if (window.XMLHttpRequest) {
// obtain new object
obj = new XMLHttpRequest();
// set the callback function
obj.onreadystatechange = processChange;
// we will do a GET with the url; “true” for asynch
obj.open(“GET”, url, true);
// null for GET with native object
obj.send(null);
// IE/Windows ActiveX object
} else if (window.ActiveXObject) {
obj = new ActiveXObject(“Microsoft.XMLHTTP”);
if (obj) {
obj.onreadystatechange = processChange;
obj.open(“GET”, url, true);
// don’t send null for ActiveX
obj.send();
}
} else {
alert(“Your browser does not support AJAX”);
}
}
function processChange() {
// 4 means the response has been returned and ready to be processed
if (obj.readyState == 4) {
// 200 means “OK”
if (obj.status == 200) {
// process whatever has been sent back here:
// we need to parse the returned data (text or XML)
// and then call checkUserName again with response set
// to the appropriate value.
// anything else means a problem
} else {
alert(“There was a problem in the returned data:n”);
}
}
}
function checkUserName(input, response) {
// if response is not empty, we have received data back from the server
if (response != ”) {
// the value of response is returned from checkName.php: 1 means in use
if (response == ‘1’) {
alert(“username is in use”);
} else {
// if response is empty, we need to send the username to the server
url = ‘http://localhost/ajaxer/checkName.php?q=’ + input;
ProcessXML(url);
}
}
}
</script>
</head>
<body>
Enter your Username: <input id=”username” name=”username” type=”text”
onblur=”checkUserName(this.value,”)” >
</body>
</html>
and my checkName.php is:
[code=php]<?php header(‘Content-Type: text/xml’);
function checkName($q) {
if (isset($q)){
switch(strtolower($q)) {
case ‘maggie’ :
return ‘1’;
break;
…
default:
return ‘0’;
}
} else {
return ‘0’;
}
}
?>
What am I missing here. At the moment I just want an alert to come up saying “user available” or “user unavailable”.