1. 用POST发送数据,在2号线函数(也是ajax发送数据的函数:ajaxCall)必须加上一句:xmlObject.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary"); //这句是必须的,解决中文乱码加密问题s
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;
}
!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;iinfo.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" >密 nbsp;码:/label> input type="password" name="psd" id="psd" width="80px" />/br>
label for="psd" >地 nbsp;址:/label>
select id="Province" onclick="getCity()">
/select>nbsp;
select id="City" onclick="getCounty()" >
/select>nbsp;
select id="County" name="xian" >
/select>
input type="submit" value="提交" />
/form>
/body>
/html>
?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;
?>
?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;
}