00001 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
00002 <html xmlns="http://www.w3.org/1999/xhtml" >
00003 <head>
00004 <title>ZIP Code to City and State using XmlHttpRequest</title>
00005 <script language="javascript" type="text/javascript">
00006 var url = "p.php?param=";
00007 function handleHttpResponse() {
00008 if (http.readyState == 4) {
00009 if (http.responseText.indexOf('invalid') == -1) {
00010
00011
00012 var city = xmlDocument.getElementsByTagName('city').item(0).firstChild.data;
00013 var state = xmlDocument.getElementsByTagName('state').item(0).firstChild.data;
00014 document.getElementById('city').value = city;
00015 document.getElementById('state').value = state;
00016
00017 isWorking = false;
00018 }
00019 }
00020 }
00021 var isWorking = false;
00022 function updateCityState() {
00023 if (!isWorking && http) {
00024 var zipValue = document.getElementById("zip").value;
00025 http.open("GET", url + escape(zipValue), true);
00026 http.onreadystatechange = handleHttpResponse;
00027 isWorking = true;
00028 http.send(null);
00029 }
00030 }
00031 function getHTTPObject() {
00032
00033 var xhr = null;
00034
00035 if(window.XMLHttpRequest)
00036 xhr = new XMLHttpRequest();
00037 else if(window.ActiveXObject){
00038 try {
00039 xhr = new ActiveXObject("Msxml2.XMLHTTP");
00040 } catch (e) {
00041 xhr = new ActiveXObject("Microsoft.XMLHTTP");
00042 }
00043 }
00044 else {
00045 alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
00046 xhr = false;
00047 }
00048 return xhr;
00049 }
00050 var http = getHTTPObject();
00051 </script>
00052 </head>
00053 <body>
00054 <form action="post">
00055 <p>
00056 ZIP code:
00057 <input type="text" size="5" name="zip" id="zip" onblur="updateCityState();" />
00058 </p>
00059 City:
00060 <input type="text" name="city" id="city" />
00061 State:
00062 <input type="text" size="2" name="state" id="state" />
00063 </form>
00064 </body>
00065 </html>