ajax zipcode完整例子

html文件代碼:

<html>
 <head>
 <title>exam</title>
 <meta http-equiv="Content-Type" content="text/html" charset="utf8">
 </head>
 <body>
 <script language="javascript">
	function ajaxFunction(){
		xmlHttp = '';
		try{
			xmlHttp=new XMLHttpRequest();
		}catch(e){
			try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				try{
					xmlHttp=new ActiveXObject("Miscrosoft.XMLHTTP");
				}catch(e){
					alert("NO AJAX");
					return false;
				}
			}
		}
		return xmlHttp;
	}
 	function callServer() {
		var xmlHttp=ajaxFunction();
 		var city = document.getElementById("city").value;
		var state = document.getElementById("state").value;
 		if((city == null)||(city=="")) return;
		if((state == null)||(state=="")) return;
		var url = "zip.php?city="+escape(city)+"&state="+escape(state);
		xmlHttp.onreadystatechange = updatePage;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
 	}
	function updatePage() {
		if(xmlHttp.readyState==4){
			var response=xmlHttp.responseText;
			document.getElementById("zipCode").value = response;
		}
	}
 </script>
 
 <form action="zip.php" method="GET">
 <p>City:<input type="text" name="city" id="city" size="25" onChange="callServer();"/></p> 
 <p>State:<input type="text" name="state" id="state" size="25" onChange="callServer();"/></p> 
 <p>zipCode:<input type="text" name="zipCode" id="zipCode" size="25" /></p>
 </form>
 </body>
</html>

php文件代碼:

<?php
	$city=$_GET['city'];
	$state=$_GET['state'];
	$output="".$city."".$state;
	echo $output;	
?>


發佈了27 篇原創文章 · 獲贊 8 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章