Monthly Archive for January, 2007

ReviewAnything

ReviewAnything was a short-lived project to set up a web application which would allow people to register user accounts and post quick reviews about anything. Users would tag reviews in any way they saw fit, be it “movies” or “downtempo.” It uses a lot of the same classes I use in other projects, which is what OOP is good for. It was also a place to experiment with AJAX, and I was able to get a type-ahead feature working for the tags. The entire thing is available here.

My First AJAX

I wrote my first bit of AJAX the other day and thought I’d post it here. We had an order form which allowed people to signup for the forums at the same time. Next to the “forum username” field I wanted a button to check if the username was available. Here’s the easy part:

<input type="button" onclick="checkUsername();" value="Check availability" />

And here’s the JavaScript that does the fancy bit:

var xmlhttp;
function checkUsername() {
    var br = navigator.appName;
    if (br == "Microsoft Internet Explorer") {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        xmlhttp = new XMLHttpRequest();
    }
 
 
// these are the fields we use to check availability
    var un = document.forms[0].vtforums_username.value;
    var em = document.forms[0].x_Email.value;
    if ((em == "") || (un == "")) {
    } else {
        var params = "username=" + un + "&email=" + em;
 
        xmlhttp.onreadystatechange = triggered;
 
 
// this is a url, with parameters, to a php script
// the php does the actually checking and returns some text
        xmlhttp.open("GET", "vtf_username_avail.php?" + params);
 
        xmlhttp.send(null);
    }
}
 
function triggered() {
// this is where we display the text we got back from our php script
    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
 
        document.getElementById("vtf_username_avail").innerHTML = xmlhttp.responseText;
    }
}