广东十一选五开奖 > 计算机网络 > ajax+php无刷新二级联动菜单的应用实例

原标题:ajax+php无刷新二级联动菜单的应用实例

浏览次数:195 时间:2019-10-03

<script language="网页特效">
var xmlhttp = null;

ajax php教程仿网易文章评论顶一下效果

</body>
</html>

<div class='plding fr'>
<a href='action.php?id="1&action=top_num'>顶一下</a>[5]
</div>

<head>

js ajax

 代码如下

<script language="网页特效" type="text/javascript">
<!--
// ajax检测评论是否已经支持过了
function getcookie(c_name){ //检测cookie
 if (document.cookie.length>0){
  c_start=document.cookie.indexof(c_name + "=")
  if(c_start!=-1){
   c_start=c_start + c_name.length+1;
   c_end=document.cookie.indexof(";",c_start);
   if(c_end==-1) c_end=document.cookie.length;
   return unescape(document.cookie.substring(c_start,c_end));
  }
 }
 return "";
}

function setcookie(c_name,value,expiredays){ //设置cookie
 var exdate=new date();
 exdate.setdate(exdate.getdate()+expiredays);
 document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.togmtstring());
}

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 get_top_num(id){ //主调函数
 var get_top_cookie=getcookie('get_top_'+id+'_cookie');
 if(get_top_cookie!=null && get_top_cookie!=""){
  alert('此条评论你已经支持过了');
 }
 else{
  var xmlhttp=createxmlhttprequest();
  var url = "action.php?action=top_num&id="+id;
  if (id==""){
   return false ;
  }
  if (xmlhttp){
   callback = getreadystatehandler(xmlhttp,id);
   xmlhttp.onreadystatechange = callback;
   xmlhttp.open("get", url,true);
   xmlhttp.send(null);
  }
  setcookie('get_top_'+id+'_cookie','istop',1);
 }
}

function getreadystatehandler(xmlhttp,id){  //服务器返回后处理函数
 var top_num = document.getelementbyid("top_num_"+id).innerhtml;
 return function (){
  if(xmlhttp.readystate == 4){
   if(xmlhttp.status == 200){
    if (xmlhttp.responsetext==1){
     document.getelementbyid("top_num_"+id).innerhtml=number(top_num)+1;
    }
   }
  }
 }
}

//-->
</script>

 代码如下

<html>
<head>
<title>ajax+php无刷新二级联动菜单的应用实例</title>

if(empty($_get['prov']))
{
    echo iconv("gb2312","utf-8",'<font color="red">您没有选择省(自治区)</font>');
}
else
{
    $prov = $_get['prov'];
    $city = $city_arr[$prov];
    echo iconv("gb2312","utf-8",'所选省(自治区)省会(首府)为:'.$city);
}
?>

<form action="www.111cn.net.php">
    <div>
    <select id="province" onchange="sendrequest()">
        <option value="">请选择一个省(自治区)</option>
        <option value="ah">安徽</option>
        <option value="fj">福建</option>
        <option value="gs">甘肃</option>
        <option value="gd">广东</option>
        <option value="gx">广西</option>
        <option value="gz">贵州</option>
        <option value="hn">海南</option>
        <option value="hb">河北</option>
        <option value="hh">河南</option>
        <option value="hl">黑龙江</option>
    </select>
    </div>
</form>

<div id="city">
</div>

function updatepage()
{
    if(xmlhttp.readystate == 4 && xmlhttp.status == 200)
    {
        var response = xmlhttp.responsetext;
        document.getelementbyid("city").innerhtml = response;
    }
}
</script>

    if((prov_name == null) || (prov_name == ""))
        return;
   
    xmlhttp = getxmlhttprequest();
    if(xmlhttp == null)
    {
        alert("浏览器不支持xmlhttprequest!");
        return;
    }

www.111cn.net.php代码

<body>
<h3>请选择一个省(自治区):</h3>

function sendrequest()
{
    var prov_name = document.getelementbyid("province").value;

function getxmlhttprequest()
{
    var xmlhttp=null;
    try
    {
        xmlhttp = new xmlhttprequest();
    }
    catch(e)
    {
        try
        {
            xmlhttp = new activexobject("msxml2.xmlhttp");
        }
        catch (e)
        {
            try
            {
                xmlhttp = new activexobject("microsoft.xmlhttp");
            }
            catch(e)
            {
                xmlhttp = false;
            }           
        }
    }

<?php
$city_arr = array(
"ah"=>"合肥",
"fj"=>"福州",
"gs"=>"兰州",
"gd"=>"广州",
"gx"=>"南宁",
"gz"=>"贵阳",
"hn"=>"海口",
"hb"=>"石家庄",
"hh"=>"郑州",
"hl"=>"哈尔滨"
);

    var url = "www.111cn.net.php";
    url = url + "?prov=" + prov_name;

return xmlhttp;
}

    xmlhttp.open("get", url, true);
    xmlhttp.onreadystatechange = updatepage;
    xmlhttp.send(null);
}

本文由广东十一选五开奖发布于计算机网络,转载请注明出处:ajax+php无刷新二级联动菜单的应用实例

关键词:

上一篇:PHP中Exception异常处理类的使用详解

下一篇:没有了