ajax+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>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不講數據庫教程連接
.net教程">www.bKjia.c0m原他