Search

For absolutely no reason whatsoever, I decided to create a simple example of AJAX, based on the framework I layed out in an earlier post.

The goal of this AJAX example is to allow a user who is registering for your site to see if the username they want to use is taken already or not, without having to submit a form and reload the page. I believe Digg uses this, and I hear it’s all the rage for Astronomy-based dating sites. You can skip to the implemented example in action here. Yeah, I did it in ASP this time, instead of PHP…FOR NO RAISIN WHATSOEVER, except maybe to show that the same JS framework works for both ASP and PHP.

First, I constructed the ASP page that will do the dynamic checking. It should take one querystring (GET) parameter, in this case “username”, and print out a message regarding whether or not that username is taken, or if it’s invalid. Here’s the gist of it:

         'if there's a space in the username, it's invalid
 	if InStr(request("username")," ") then
 		response.write "TakenOrNot|Spaces are not allowed in usernames, please try again."
 	else
 	        'database stuff
 	        dim strConn, dbConn, RS
                 '...set up your connection string here (MySQL, MS-SQL, whatever)
 	 	dbConn.open(strConn)

 	 	'query the database
 	 	set RS = dbConn.execute("SELECT count(*) AS cnt FROM blog_comment " &_
    					      "WHERE username = " & request.querystring("username"))

 		'is this name taken or not
 		if RS("cnt") > 0 then
 			response.write "TakenOrNot|Sorry, " & request.querystring("username") &_
                                  " is already taken.  Please try a different one."
 	        else
 			response.write "TakenOrNot|" & "Congratulations!  " & request.querystring("username") &_
                                  " is available for you to use."
 		end if
 	end if
 

So, depending on the username, this ASP code will print 1 of 3 possible strings.

The next thing I did was to write a new function in my ajax.js file. This is very similar to the sndReq() function from before:

 // this function should be called for user input
 // it opens up the usercheck.asp page with a querystring of 'action'
 function sndUserCheck(action)
 {
      http.open("get", "usercheck.asp?username=" + action);
      http.onreadystatechange = handleResponse;
      http.send(null);
 }
 

So, whenever this function is called, it will send an XMLHttpRequest to usercheck.asp using whatever username string was specified by whatever is calling this function. Not much different than sndReq(). One could probably abstract this function out even more to make the ASP/PHP page name a parameter as well, but for simplification, I won’t do that.

Finally, I set up the interface page:

 <HTML>
 <TITLE>AJAX Username Checker Example</TITLE>

 <!-- AJAX functionality -->
 <script type="text/javascript" src="ajax.js"></script>

 <BODY>

 <form name="FormAccount">
 	<input type="text" id="formUserName" name="formUserName">
 	<input type="button" value="Check" onclick="sndUserCheck(document.FormAccount.formUserName.value);">
 </form>

 	<br><br>

 	<div id="TakenOrNot"></div>

 </BODY>
 </HTML>

Again, not much different than the original example. We have one text box, where the user types in the name they want to check (formUserName), we have one button that will they will click to do the checking (formCheck), and one div object that will display the response message. Note that we grab the user’s text input for the sndUserCheck function like so:

document.FormAccount.formUserName.value

Note the use of the FormAccount (the name of the form) and formUserName (the ID of the text box)

That’s all there is to it! Users are now able to look up a username in your database and give a response to the user without a refresh. You can download the source code for this example below.

Digg this story



45 Responses to “AJAX username availability checking”

Leave a Reply