AJAX Code
This lesson shows you the code that was used to generate the example in the previous lesson.
The code
The Form
<cfquery name="getCountries" datasource="ajax_example">
select * from Countries
order by CountryName asc
</cfquery>
<script src="selectRegions.js"></script>
<form>
<div>
<span>
<b>Select a country:</b>
</span>
<span>
<select name="Country" onChange="showRegions(this.value)">
<cfoutput query="getCountries">
<option value="#CountryCode#">#CountryName#</option>
</cfoutput>
</select>
</span>
</div>
<div id="regionList">
</div>
</form>
This code does the following:
- ColdFusion's
cfqueryqueries the database for a list of countries. - The
scripttag links to a JavaScript file containing our AJAX code. - The
form,selectandoptiontags create a form. TheonChangeevent handler calls theshowRegions()function from the .js file. This only executes when the user selects a country. - The
div id="regionList"is a place holder for the regions select list. When the AJAX code is executed, this div will contain regions based on the country selected.
var oXmlHttp
function showRegions(str)
{
var url="/ajax/get_regions.cfm?&countryCode=" + str
oXmlHttp=GetHttpObject(stateChanged)
oXmlHttp.open("GET", url , true)
oXmlHttp.send(null)
}
function stateChanged()
{
if (oXmlHttp.readyState==4 || oXmlHttp.readyState=="complete")
{
document.getElementById("regionList").innerHTML=oXmlHttp.responseText
}
}
function GetHttpObject(handler)
{
try
{
var oRequester = new XMLHttpRequest();
oRequester.onload=handler
oRequester.onerror=handler
return oRequester
}
catch (error)
{
try
{
var oRequester = new ActiveXObject("Microsoft.XMLHTTP");
oRequester.onreadystatechange=handler
return oRequester
}
catch (error)
{
return false;
}
}
}
This JavaScript code does the following:
- The
showRegionsfunction uses theopen()method to initialise the connection. Thesend()method can be used for sending extra data but we don't have any to send (hence "null"). - The
stateChangedfunction uses thereadystatevariable of the XMLHttpRequest object to determine when the server has been contacted and the data retrieved. It then populates the "regionList" div with the data retrieved by the get_regions.cfm page. It does this by using theresponseTextmethod. - The
GetHttpObjectfunction creates an XMLHttpRequest object. Well, it actually has a couple of tries usingtryandcatchfor browser compatibility reasons. Microsoft IE creates XMLHttpRequest as an ActiveX object while most other browsers do it the other way. Some don't support the XMLHttpRequest object.
<cfquery name="getRegions" datasource="ajax_example">
select * from Regions where CountryCode = '#url.countryCode#'
</cfquery>
<cfif getRegions.recordcount>
<span>
<b>Select a region:</b>
</span>
<span>
<select name="Regions">
<cfoutput query="getRegions">
<option value="#RegionCode#">#RegionName#</option>
</cfoutput>
</select>
</span>
</cfif>
This code does the following:
cfqueryqueries the database. We are looking for all regions for this country code (as passed in via the URL).- If the
getRegionsquery returned at least one record, we loop through the results, populating an HTMLselectandoption.

