AJAX JavaScript
This is the JavaScript code stored in the file "selectcustomer_xml.js:
var xmlHttpfunction showCustomer(str){ xmlHttp=GetXmlHttpObject();if (xmlHttp==null) { alert ("Your browser does not support AJAX!"); return; }var url="getcustomer_xml.asp";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("GET",url,true);xmlHttp.send(null);}function stateChanged() { if (xmlHttp.readyState==4){var xmlDoc=xmlHttp.responseXML.documentElement;document.getElementById("companyname").innerHTML=xmlDoc.getElementsByTagName("compname")[0].childNodes[0].nodeValue;document.getElementById("contactname").innerHTML=xmlDoc.getElementsByTagName("contname")[0].childNodes[0].nodeValue;document.getElementById("address").innerHTML=xmlDoc.getElementsByTagName("address")[0].childNodes[0].nodeValue;document.getElementById("city").innerHTML=xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;document.getElementById("country").innerHTML=xmlDoc.getElementsByTagName("country")[0].childNodes[0].nodeValue;}}function GetXmlHttpObject(){var xmlHttp=null;try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } }return xmlHttp;}
ShowCustomer () and GetXmlHttpObject () are the same as those in the previous section. The stateChanged () function was used earlier in this tutorial. However, this time we use responseXML to return results in an XML document and use DOM to extract the value to be displayed.
AJAX Server Page
The server page called by JavaScript is a simple ASP file named "getcustomer_xml.asp.
This page is written in VBScript for Internet Information Server (IIS ). You can simply rewrite the page in PHP or other server languages.
See the corresponding example in PHP (test: missing a specific page ).
This code executes the SQL query for the database and returns the result in XML:
<%response.expires=-1response.contenttype="text/xml"sql="SELECT * FROM CUSTOMERS "sql=sql & " WHERE CUSTOMERID='" & request.querystring("q") & "'"on error resume nextset conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open(Server.Mappath("/db/northwind.mdb"))set rs=Server.CreateObject("ADODB.recordset")rs.Open sql, connif err <> 0 thenresponse.write(err.description)set rs=nothingset conn=nothingelseresponse.write("<?xml version='1.0' encoding='ISO-8859-1'?>")response.write("<company>")response.write("<compname>" &rs.fields("companyname")& "</compname>")response.write("<contname>" &rs.fields("contactname")& "</contname>")response.write("<address>" &rs.fields("address")& "</address>")response.write("<city>" &rs.fields("city")& "</city>")response.write("<country>" &rs.fields("country")& "</country>")response.write("</company>")end ifon error goto 0%>
Note the second line in the above ASP code: response. contenttype = "text/xml ". The ContentType attribute sets the HTTP content type for the response object. The default value of this attribute is "text/html ". This time, we set the content type to XML.
Then we select data from the database and use the data to build the XML document.