/    Sign up×
Community /Pin to ProfileBookmark

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>[/code]

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’;
}

}
?>[/code]

What am I missing here. At the moment I just want an alert to come up saying “user available” or “user unavailable”.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@cluefulMay 09.2011 — Below is your function reformatted but unchanged.

The onblur handler calls this function with the second parameter set to "".

What's going to happen?
[CODE]function checkUserName(input, response)
{
if (response != '')
{
if (response == '1')
{
alert("username is in use");
}
else
{

url = 'http://localhost/ajaxer/checkName.php?q=' + input;
ProcessXML(url);
}
}
}[/CODE]
Copy linkTweet thisAlerts:
@mioceneauthorMay 09.2011 — It should call the processXML function, sending the url with the username in it as the parameter.
Copy linkTweet thisAlerts:
@cluefulMay 09.2011 — It should call the processXML function, sending the url with the username in it as the parameter.[/QUOTE]
Well if you follow it through, it should be perfectly clear that it's not going to do that.
Copy linkTweet thisAlerts:
@tha_oneMay 09.2011 — What does this:
[CODE]checkUserName(any_input_field.value, "")[/CODE]
?

Nothing, right?

And what's this?
[code=html]<input id="username" name="username" type="text"
onblur="checkUserName(this.value,'')" >[/code]
Copy linkTweet thisAlerts:
@mioceneauthorMay 09.2011 — OK, I see now that the if statements are wrongly nested. I'll fix that tomorrow.

But saying that is fixed, how do I retrieve the "1" or "0" from the checkname.php and use it in the page? Say to generate an alert box?
Copy linkTweet thisAlerts:
@cluefulMay 10.2011 — You can do it as below. Your use of a single function for two purposes entails careful type checking:[CODE]<!DOCTYPE html>
<html>

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Ajaxer</title>

<script type="text/javascript">

function ProcessXML(url)
{
var obj;

if (window.XMLHttpRequest)
obj = new XMLHttpRequest();

else
if (window.ActiveXObject)
obj = new ActiveXObject( "Microsoft.XMLHTTP" );

if (obj)
{
obj.onreadystatechange = processChange;
obj.open("GET", url, true);

obj.send( null );
}
else
alert("Your browser does not support AJAX");

}

function processChange()
{
var n;

if( this.readyState == 4 )
if( this.status == 200 )
{
if( isNaN( n = parseInt( this.responseText ) ) )
alert("There was a problem in the returned data ");
else
checkUserName( null, n );
}
else
alert( 'Server communication error' );
}

function checkUserName(input, response)
{
if( response !== '' )
{

alert( "Username is " + ( response === 1 ? "" : "not " ) + "in use" );

}

else
{
ProcessXML( 'http://localhost/ajaxer/checkName.php?q=' + input );

}

}

</script>

</head>

<body>
Enter your Username: <input id="username" name="username" type="text"
onblur="checkUserName(this.value,'')" >
</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@mioceneauthorMay 10.2011 — OK thanks for your help so far. I've got the AJAX side working and rather than have an alert popping up, which is quite frankly really annoying, I'm trying to make a div change color. So I've written a function to do this, using a div that I've put into the html:

[code=html]<div id="indicator" style="width:20px;height:20px"></div>[/code]

[CODE]function changeIndicator(inuse){
var ind_div = document.getElementById('indicator');

if (inuse == "1"){
ind_div.style.background = "red";
}
else if (inuse == "0"){
ind_div.style.background = "green";
}

}[/CODE]


And then I call this function here:

[CODE]function checkUserName(input, response) {

if (response != '') {

changeIndicator(response);

} else {

url = 'http://localhost/ajaxer/checkName.php?q=' + input;
ProcessXML(url);
}

}[/CODE]


My php is stronger than my javascript. Am I missing something elementary here?
Copy linkTweet thisAlerts:
@mioceneauthorMay 10.2011 — Also, although I said I got the AJAX bit working, I keep on getting the "There was a problem in the returned data" alert.

Checking shows that the responseText is null. Is this an issue with the way the server-side script is put together?
×

Success!

Help @miocene spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...