当前位置: 首页 > 工具软件 > easy-ajax > 使用案例 >

简单AJAX

司空祯
2023-12-01

//*------------------js-------------------*//

var MyID,PanlName; //ID和数字容器名称前缀
//创建XMLHttpRequest对象
xmlHttp=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Opera, etc.
  request=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  request=new ActiveXObject("Microsoft.XMLHTTP");
  }

if (request==null)alert("Error initializing XMLHttpRequest!");

//获取信息
function GetNum(id,panlname) {
  MyID=id;
  PanlName=panlname;
  var url = "/GetNum.asp?id="+id+"&panlname="+panlname;
  request.open("GET", url, true);
  request.onreadystatechange = updatePage;
  request.send(null);
}
//更新页面
function updatePage() {
if (request.readyState == 4) {
  if (request.status == 200) {
  var response = request.responseText; 
  document.getElementById(PanlName+MyID).innerHTML = response;
  }else if(request.status == 404) {
  alert ("Requested URL is not found.");
  } else if (request.status == 403) {
  alert("Access denied.");
  } else
  alert("status is " + request.status);
  }
}

//*------------------GetNum.asp代码-------------------*//

<!--#include file="conn1.asp" -->
<%'更新数据库
    if request("panlname")="TnumUp" then
       sql="update Article set numUp=iif(isnull(numUp),0,numUp)+1 where  id="&request("id")
    sql2="Select  numUp as num From Article where  id="&request("id")
 elseif request("panlname")="TnumMsg" then
    sql="update Article set numMsg=iif(isnull(numMsg),0,numMsg)+1 where  id="&request("id")
    sql2="Select  numMsg as num From Article where  id="&request("id")
 elseif request("panlname")="TnumShare" then
    sql="update Article set numShare=iif(isnull(numShare),0,numShare)+1 where  id="&request("id")
    sql2="Select  numShare as num From Article where  id="&request("id")
 end if
 Set Rs=Conn.Execute(sql)
  '查询数据库 
 
 Set Rs=Conn.Execute(sql2)
 if not rs.eof and not rs.bof then
    num=rs("num")
 end if
 response.Write(num)
%>

//*------------------调用方法-------------------*//

 οnclick="GetNum(537,'TnumUp')"

 类似资料: