自做AJAX之投票系统
(自学真难,写出来也难。题外话^_^),第一次写的东西,不太好,过路高手指点啦。
自做AJAX之投票系统,无刷新调取其它页面数据。
JavaScript语言,.js文件内容:
var xmlHttp = false; //
//初始化XMLHttpRequest对象
function createXmlHttp(){
xmlHttp = false;
if (window.ActiveXObject) {
try {xmlHttp = new ActiveXObject(“Msxml2.XMLHTTP”);}
catch (e) {xmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”);}
}else if (window.XMLHttpRequest) {xmlHttp = new XMLHttpRequest();}
}
function sendRequest(idnum,strType){
createXmlHttp();
var url = “toupiao.asp?downId=” + idnum + “&piaoType=” + strType+”×tamp=” + new Date().getTime();
if (!xmlHttp) {
alert(“XMLHttpRequest 创建失败!”);
}
xmlHttp.open(“GET”, url, false);
xmlHttp.onreadystatechange = function(){//回调函数开始
var goodimg,goods,badimg,bads;
goodimg = document.getElementById(“goodimg”);
goods=document.getElementById(“goodSpan”);
badimg = document.getElementById(“badimg”);
bads=document.getElementById(“badSpan”);
//alert(xmlHttp.responseText);
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var requTxt= xmlHttp.responseText;
if (requTxt==”true”)
{
alert(“你已投过票!请20分钟之后再继续。”);
}else{
var splitTxt=requTxt.split(“###”);
var pa=parseInt(splitTxt[0]);
var pb=parseInt(splitTxt[1]);
var requTxt=pa + pb;
var num=((pa/requTxt)*100)/2;
var num1;
if (Math.round(num)==0 || Math.round(num)==””)
{
num1=”0″;
}else num1=Math.round(num);
var num2=num1 + “.” + shudian(num) + ” %”;
goodimg.style.width=num1;
goods.innerText=num2;
num=((pb/requTxt)*100)/2;
if (Math.round(num)==0 || Math.round(num)==””)
{
num1=”0″;
}else num1=Math.round(num);
num2=num1 + “.” + shudian(num) + ” %”;
badimg.style.width=num1;
bads.innerText=num2;
alert(“投票成功!”);
}
//alert(num2);
}
}//回调函数结束
xmlHttp.send(null);
}
function shudian(a){//取小数点后2位
var i=a.toString().indexOf(“.”);
var a1=a.toString().substring(i+1,i+3);
return a1;
}
ASP数据文件内容:
<%
Dim rs,db,conn,str
conn=server.mappath(“database/shangpin.mdb”)
set db=server.createobject(“adodb.connection”)
db.open “driver={microsoft access driver (*.mdb)};dbq=”&conn
Set rs=server.CreateObject(“adodb.recordset”)
If Err Then
Err.clear
response.write “数据库链接错误,请查询数据库链接语句。”
Call dbclose()
response.End
End If
Function dbclose()
Set rs=Nothing
db.close
Set db=Nothing
End function
Dim downId,piaoType
downId=Trim(request(“downId”))
piaoType=Trim(request(“piaoType”))
´downId=6
´piaoType=”good”
If downId<>”” And IsNumeric(downId) Then
str=”select shang_piao_ip,shang_piao_date from shangpin where id=”&downId
rs.open str,db
Dim shangIp,shangDate,piaoId
If Not rs.eof Or Not rs.bof Then
shangIp=rs(“shang_piao_ip”)
shangDate=rs(“shang_piao_date”)
End If
rs.close
Dim ip
ip=request.serverVariables(“REMOTE_ADDR”)
If ip=shangIp And DateDiff(“n”,shangDate,Now())<20 Then
response.write “true”
Else
If piaoType<>”” Then
If piaoType=”good” Then
str=”update shangpin set shang_piao_good=shang_piao_good+1,shang_piao_ip=´”& request.serverVariables(“REMOTE_ADDR”) &”´,shang_piao_date=´”& Now() &”´ where id=”&downId
db.execute(str)
str=”select * from shangpin where id=”&downId
rs.open str,db
response.write CStr(rs(“shang_piao_good”)) & “###” & CStr(rs(“shang_piao_bad”))
rs.close
Set rs=Nothing
db.close
Set db=Nothing
Else
str=”update shangpin set shang_piao_bad=shang_piao_bad+1,shang_piao_ip=´”& request.serverVariables(“REMOTE_ADDR”) &”´,shang_piao_date=´”& Now() &”´ where id=”&downId
db.execute(str)
str=”select * from shangpin where id=”&downId
rs.open str,db
response.write CStr(rs(“shang_piao_good”)) & “###” & CStr(rs(“shang_piao_bad”))
rs.close
Set rs=Nothing
db.close
Set db=Nothing
End If
End If
End If
End If
%>
今天的文章自做asp与AJAX之投票系统分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/33011.html