网络编程
位置:首页>> 网络编程>> 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
投稿

猜你喜欢

  • 1、看机器配置,指三大件:cpu、内存、硬盘2、看mysql配置参数3、查系mysql行状态,可以用mysqlreport工具来查看4、查看
  • 在Linux下C连接MySQL出现问题如下:编译成功后,运行程序,出现./connect: error while loading shar
  • CSS圆角的现实一直是大家所热衷的话题,我们进行CSS布局一直强调语义,强调文档的结构。圆角作为页面的外面表现,应该分离到CSS文件中,可以
  • 很多时候关心的是优化SELECT 查询,因为它们是最常用的查询,而且确定怎样优化它们并不总是直截了当。相对来说,将数据装入数据库是直截了当的
  • 不知道用ASP写代码的朋友是不是和我有一样的感受,ASP中最头疼的就是调试程序的时候不方便,我想可能很多朋友都会用这样的方法&ldq
  • 选择自 xinyuxin912 的 Blog将一个图片以二进制值的形式存入Xml文件中try   { &nbs
  • 在讨论IE6的BUG及如何修复之前,有必要讲叙一些策略去避免这些恼人的问题——正所谓防患于未然 。IE6 市场占有率据Market Shar
  • 最近对微格式进行了一些学习,在学习过程中收获不少。在此分享下,欢迎交流!微型格式的优点:1,语义化的HTML和CSS类名称来标记共同内容。2
  • tbody 标签表格主体(正文)。该标签用于组合 HTML 表格的主体内容。tbody 元素应该与&
  • 密码保护是注册过程中的关键环节,尤其是对帐户安全级别比较高的网站,尤其在账号被盗或者涉及安全登录等问题的情况下,密码保护问题作为用户身份识别
  • 代码如下:<% FunctIon DownloadFIle(StrFIle) StrFIlename=StrFIle Response
  • 请问如何用OleDbDataAdapter来对数据库进行删除、修改和添加?OleDbDataAdapter是DataSet和数据源之间建立联
  • 有很多应用项目, 刚起步的时候用MYSQL数据库基本上能实现各种功能需求,随着应用用户的增多,数据量的增加,MYSQL渐渐地出现不堪重负的情
  • 三遍记忆,让你记住海量素材的准确位置和用途,提高其可用性.这仅仅是一个示意图.在以往的日子里,我做到了只要脑袋里冒出一个想法,立刻就能知道我
  • 相信没有人不知道 Firebug 是什么东西,但有时候我们糟糕的代码不想让同行轻松的使用 F12 就能一览无遗。那么怎么办呢?这里有个猥琐的
  • 在用 Javascript 验证表单(form)中的单选框(radio)是否选中时,很多新手都会遇到问题,原因是 radio 和普通的文本框
  • 一、数组的创建方式一var a = new Array(); a[0]="wo"
  • 一、oracle oracle服务器有Oracle instace 和Oracle database instance有memory str
  • 这个技巧将教你如何用css做出漂亮的文本按钮,有活力的按钮将节省你很多制作图片的时间,也能让你一天的工作中成为一个快乐的人,让我们一起看看效
  • 随着ajax再网站建设中的的大范围流行,XmlHttp也自然被人们所熟悉。本文介绍了asp任何检测服务器端是否支持xmlhttp组件的方法,
手机版 网络编程 asp之家 www.aspxhome.com