Home  >  Article  >  Backend Development  >  Ajax+php calls classic example without refreshing data_PHP tutorial

Ajax+php calls classic example without refreshing data_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:06:591206browse

ajax+php教程 无刷新数据调用经典实例




ajax+php 无刷新数据调用经典实例
<script><br> function G(id){<br>   return document.getElementById(id);<br>  }<br>  <br>  function createXMLHttpRequest(){//创建XMLHttpRequest对象<br>   if(window.ActiveXObject){//IE<br>     try {<br>      return new ActiveXObject("Microsoft.XMLHTTP");<br>     } catch(e){<br>      return;<br>     }<br>    }else if(window.XMLHttpRequest){//Mozilla,firefox<br>     try {<br>      return new XMLHttpRequest();<br>     } catch(e){<br>      return;<br>     }<br>    }<br>   }</p> <p> function returnCity(Value,divId,at,pid){//主调函数<br>   var xmlHttp=createXMLHttpRequest();<br>   var url = "s.php?cid="+Value+"&at="+at+"&pid="+pid+"&mt="+Math.random(100);<br>   //alert(url);<br>   if (Value==""){  <br>    return false ;<br>   }<br>   if (xmlHttp){<br>    callback = getReadyStateHandler(xmlHttp,divId);<br>    xmlHttp.onreadystatechange = callback;<br>    xmlHttp.open("GET", url,true);<br>    xmlHttp.send(null);<br>   }<br>  }</p> <p> function getReadyStateHandler(xmlHttp,divId){//服务器返回后处理函数<br>   return function (){<br>    if(xmlHttp.readyState == 4){<br>     if(xmlHttp.status == 200){        <br>       if (xmlHttp.responseText){<br>        G(divId).innerHTML=xmlHttp.responseText;     <br>       }else{<br>        G(divId).innerHTML="";     <br>       }      <br>     }<br>    }<br>   }<br>  }<br>  <br>  function ajaxData(inputName,divId,action,cpid)<br>  {<br>   //alert(inputName);<br>   var cityid = G(inputName).value;<br>   var pid = G(cpid).value;  <br>   returnCity(cityid,divId,action,pid);</p> <p> }<br> </script>







//s.php file
$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 .= "
  • ".$v['city_price_show']."
  • "; 
    }
    echo $str;
    }
    else
    {
    exit('optional!');

    //Here we only talk about ajax and not about database connection
    .net tutorial">www.bKjia.c0m original him

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630585.htmlTechArticleajax+php tutorial no refresh data call classic example! DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional //EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd html xmlns=http...
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn