注意點:
1. 用POST發送數據,在2號線函數(也是ajax發送數據的函數:ajaxCall)必須加上一句:xmlObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
接著使用xmlObject.send(data);發送
2.3號線函數要注意:
1.禁用緩存(建議,不必要):header("Cache-Control:no-cache");
2.使用XML數據格式必須加上:header("Content-Type: text/xml; charset=gb2312");//這裡要寫XML
3.若使用WAMP5集成環境安裝的MYSQL,在查詢數據庫時候,必須加上:
$charset = "gb2312";
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary"); //這句是必須的,解決中文亂碼加密問題s
否則就會亂碼加密,今天我就是在這裡浪費了很久時間,我是用ECSHOP GBK版 默認安裝的數據庫
4.若用XML接受數據,回調函數必須分IE和非IE處理,否則總是有一方娶不到XML數據
處理代碼如下:
復制代碼 代碼如下:
function getXMLData(tagName)//獲取XML數據,分IE和非IE處理
{
var info;
if(window.ActiveXObject) //IE取回XML文件方法
{
var doc = new ActiveXObject("MSxml2.DOMDocument");
doc.loadXML(xmlObject.responseText);
info = doc.getElementsByTagName(tagName);
}
else //---------------------------非IE取回XML文件方法
{
info = xmlObject.responseXML.getElementsByTagName(tagName);
}
return info;
}
下面就是我做的一個省市聯動測試
代碼如下:
index.php
復制代碼 代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>省事聯動測試</title>
<style type="text/css" >
select{
width:100px;
}
</style>
<script type="text/javascript" >
var thisId = ""; //當前操作的selectI的D
var xmlObject; //ajax 對象全局變量,
function getAjaxObject()//AJAX 1號線,返回一個AJAX 對象引擎
{
var xmlObject ;
if(window.ActiveXObject)
{
xmlObject = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
xmlObject = new XMLHttpRequest();
}
return xmlObject ;
}
function ajaxCall(id) //ajax 二號線 ,這裡采用 post 傳遞參數
{
xmlObject = new getAjaxObject();
if(xmlObject)
{
var url = "chuli.php";
var data = "id=" + id;
xmlObject.open("post",url,true);
xmlObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlObject.onreadystatechange = repayFuncion;
xmlObject.send(data);
}
}
function repayFuncion() //ajax 四號線 ,這裡采用 xml 接受數據,這裡還涉及到xmldom編程
{
if(xmlObject.readyState==4 && xmlObject.status==200)
{
var info = getXMLData("res");//獲取XML數據
$(thisId).length = 0;//清楚select 中的option節點
for(i=0;i<info.length;i++)
{
var optionId = info[i].childNodes[0].childNodes[0].nodeValue;
var optionValue = info[i].childNodes[1].childNodes[0].nodeValue;
var optionNode = document.createElement('option');
optionNode.value = optionId;
optionNode.innerText =optionValue;
$(thisId).appendChild(optionNode);
}
}
}
function getXMLData(tagName)//獲取XML數據,分IE和非IE處理
{
var info;
if(window.ActiveXObject) //IE取回XML文件方法
{
var doc = new ActiveXObject("MSxml2.DOMDocument");
doc.loadXML(xmlObject.responseText);
info = doc.getElementsByTagName(tagName);
}
else //---------------------------非IE取回XML文件方法
{
info = xmlObject.responseXML.getElementsByTagName(tagName);
}
return info;
}
function $(id)//常用函數,通過ID取對象
{
return document.getElementById(id);
}
function getProvice()//獲取省
{
thisId = "Province";
var id = '1';
ajaxCall(id);
}
function getCity()//獲取市
{
thisId = "City";
$("County").length = 0;
var id = $("Province").value;
ajaxCall(id);
}
function getCounty()//獲取縣城
{
thisId = "County";
var id = $("City").value;
if($("City").length)
{
ajaxCall(id);
}
}
window.onlaod = getProvice();//頁面開始載入省
</script>
</head>
<body>
<form action="javascript:void(0)" method="post">
<label for="username" >用戶名:</label> <input type="text" name="username" id="username" width="60px" /><br />
<label for="psd" >密 碼:</label> <input type="password" name="psd" id="psd" width="80px" /></br>
<label for="psd" >地 址:</label>
<select id="Province" onclick="getCity()">
</select>
<select id="City" onclick="getCounty()" >
</select>
<select id="County" name="xian" >
</select>
<input type="submit" value="提交" />
</form>
</body>
</html>
chuli.php
復制代碼 代碼如下:
<?php
//3號線
header("Cache-Control:no-cache");
header("Content-Type: text/xml; charset=gb2312");//這裡要寫XML
require("function.php");
$id = $_POST['id'];
file_put_contents("my1.txt",$act . "------" . $ziduan);
$result = getresultById($id);
$info = "<mes>";
foreach($result as $row)
{
$info .= "<res>";
$info .= "<id>" . $row['region_id'] . "</id>";
$info .= "<name>" . $row['region_name'] . "</name>";
$info .= "</res>";
}
$info .= "</mes>";
echo $info;
?>
3.數據庫函數
function.php
復制代碼 代碼如下:
<?php
function getresultById($id)
{
$con = mysql_connect("localhost","root","");
if($con)
{
$charset = "gb2312";
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary"); //這句是必須的,解決中文亂碼加密問題s
mysql_select_db("ajax",$con);
$sql = "select * from ecs_region where parent_id = '$id'";
$res = mysql_query($sql);
$arr = array();
while($row = mysql_fetch_assoc($res))
{
$arr[] = $row;
}
return $arr;
}
return false;
}