<!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>ajax+php 无刷新数据调用经典实例</title>
<script>
function G(id){
return document.getElementById(id);
}
function createXMLHttpRequest(){//创建XMLHttpRequest对象
if(window.ActiveXObject){//IE
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch(e){
return;
}
}else if(window.XMLHttpRequest){//Mozilla,firefox
try {
return new XMLHttpRequest();
} catch(e){
return;
}
}
}
function returnCity(Value,divId,at,pid){//主调函数
var xmlHttp=createXMLHttpRequest();
var url = "s.php?cid="+Value+"&at="+at+"&pid="+pid+"&mt="+Math.random(100);
//alert(url);
if (Value==""){
return false ;
}
if (xmlHttp){
callback = getReadyStateHandler(xmlHttp,divId);
xmlHttp.onreadystatechange = callback;
xmlHttp.open("GET", url,true);
xmlHttp.send(null);
}
}
function getReadyStateHandler(xmlHttp,divId){//服务器返回后处理函数
return function (){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 200){
if (xmlHttp.responseText){
G(divId).innerHTML=xmlHttp.responseText;
}else{
G(divId).innerHTML="";
}
}
}
}
}
function ajaxData(inputName,divId,action,cpid)
{
//alert(inputName);
var cityid = G(inputName).value;
var pid = G(cpid).value;
returnCity(cityid,divId,action,pid);
}
</script>
</head>
<body>
<input name="" type="text" onMouseOver="ajaxData('city_type','cityareaid','c','borough_type');" />
<div id="ajaxHtml">
</div>
</body>
</html>
//s.php文件
$Db = new Db();
$type = ( $action=='esfPice')?1:2;
$sql = "Select * from cn_loupan_city_price where city_id ='$cid' and price_type='$type'";
$query = $Db->query( $sql );
$str ='';
if( $Db->rows( $query ) )
{
$result = $Db->fetch( $query ,0);
foreach( $result as $_v => $v )
{
$str .= "<LI><A onClick="switchprop('price2_Value','".$v['city_price_show']."','price2_List','price','".$v['city_price']."')" href="javascript教程:void(0);" target=_self>".$v['city_price_show']."</A></LI>";
}
echo $str;
}
else
{
exit('可不选!');
}
//这里只讲ajax不讲数据库教程连接
www.111cn.net原他