网络编程
位置:首页>> 网络编程>> JavaScript>> ajax实现Dig程序中的投票

ajax实现Dig程序中的投票

作者:无情 来源:设计家园 发布时间:2008-01-22 17:27:00 

标签:ajax,投票,dig

asp+js做的一个dig程序中的投票(有的叫顶一下,踩一下),由于代码较长,只贴出核心部分:投票中的代码

相关文章推荐:ajax +asp 星级投票/留言系统

网页显示投票的部分:

<div class="Vote1"><script src='/voteResult.asp?id=1&action=view'></script></div>

效果如图:,然后点投一票,票数增加1,如图:

voteResult.asp代码

<%
Response.ContentType="text/html; charset=gb2312"
'链接数据库代码省略
set rs=server.createobject("adodb.recordset")
sql=("select news_id,vote from dwww_news where news_id="&request("id")&"")
rs.open sql,conn,1,1
if not rs.eof and not rs.bof Then '先检查传过来的id是否正确
if request("action")="view" Then '如果是新闻代码中传来的id
if instr(request.Cookies("voteid"),request("id")&"|")<>0 Then '判断该id的新闻是否投过票
response.write "document.write (""<div class='result' id='result_"&rs("news_id")&"'>"&rs("vote")&"</div>"");"
response.write "document.write (""<span class='result_link' id='result_link_"&rs("news_id")&"'>投票成功</span>"");"
else
response.write "document.write (""<div class='result' id='result_"&rs("news_id")&"'><a href='javascript:dovote("&request("id")&")'>"&rs("vote")&"</a></div>"");"
response.write "document.write (""<span class='result_link' id='result_link_"&rs("news_id")&"'><a href='javascript:dovote("&request("id")&")'>投一票</a></span>"");"
end if 
Else '点击投票处理代码
if instr(request.Cookies("voteid"),request("id")&"|")<>0 then
Response.Cookies("voteid")=request("id")&"|"
Response.Cookies("voteid").Expires=Date()+365
else
Response.Cookies("voteid")=request("id")&"|"&request.Cookies("voteid")
Response.Cookies("voteid").Expires=Date()+365
end If 

vote=rs("vote")+1
conn.execute ("update dwww_news set vote=vote+1 where news_id="&request("id")&"")
response.write ""&vote&""
end If 
end if 
rs.close()
Set rs=Nothing
'关闭数据库代码省略
%>

其中程序中用到的css代码:

voteBg.gif 图片:520)this.width=500" border=0>

.Vote1{float:left;width:54px;height:71px;background:url(/images/voteBg.gif);margin:0 5 0 0;text-align:center; }
.DivAllShow2 .Vote1{float:left;width:54px;height:71px;background:url(/images/voteBg.gif);margin:6 5 0 0;text-align:center; }
.vote1 div{height:44px;line-height:44px;font-size:18px; font-weight:bold;font-family:Georgia;color:#333333;}
.result a:link{font-size:18px; font-weight:bold;text-decoration:none;}
.result a:visited {font-size:18px; font-weight:bold;text-decoration:none;}
.result a:hover {font-size:18px; font-weight:bold;text-decoration:none;}
.Vote1 span{height:20px;line-height:20px;color:#CE4A02;margin:5 0 0 0;}
.result_link a:link{color:#CE4A02;text-decoration:none;}
.result_link a:visited {color:#CE4A02;text-decoration:none;}
.result_link a:hover {color:#CE4A02;text-decoration:none;}

js代码:

var http_request = false;
function send_request(url,votei) {//初始化、指定处理函数、发送请求的函数
http_request = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}

http_request.onreadystatechange = function(){processRequest(votei)}; 
// 确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open("post", url, true);
http_request.send(null);

}
// 处理返回信息的函数
function processRequest(votei) {
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
//alert(http_request.responseText);
document.getElementById('result_'+votei).innerHTML=http_request.responseText;;
document.getElementById('result_link_'+votei).innerHTML = "投票成功";
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}
function dovote(votei) 
{
var votei=votei;
send_request('/voteResult.asp?id='+escape(votei),votei);
}
function getObj(objName){return(document.getElementById(objName));}

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com