Ajax实现网上投票(NICE)

 AjaxVote.JS

 

// JavaScript Document

//提交函数checkAndSubmit(int投票ID,this.form.单选框名字,str表单div ID,str显示结果div ID,int每项的高度)
//查看函数callServer('',int投票ID,str表单div ID,str显示结果div ID,int每项的高度)  第一个是''

var request = false;
try {
  request = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      request = false;
    }
  }
}
if (!request)
  alert("Error initializing XMLHttpRequest!");
var formDiv1;
var resultDiv1;
function callServer(strValue,whatVote,formDiv,resultDiv,itemHeight) {
  // Get the city and state from the web form
  var strValue = strValue;
  formDiv1=formDiv;
  resultDiv1=resultDiv;
  // Only go on if there are values for both fields
  // Build the URL to connect to
  var url = 'voteAndview.asp?strValue=' + escape(strValue)+'&whatVote='+whatVote+'&formDiv='+ escape(formDiv)+'&resultDiv='+ escape(resultDiv)+'&itemHeight='+itemHeight;
  // Open a connection to the server
  request.open("GET", url, true);
  // Setup a function for the server to run when it's done
  request.onreadystatechange = updatePage;
  // Send the request
  request.send(null);
}

function updatePage() {
  if (request.readyState == 4) {
 
 if (request.status == 200){
    var response = request.responseText.split("|");
 
 if (response[1])
 alert("您已经投过票了!");
 document.getElementById(formDiv1).style.display="none";
 document.getElementById(resultDiv1).style.display="";
 document.getElementById(resultDiv1).innerHTML = response[0];
 }
 else if (request.status == 404){
         alert("Request URL does not exist");}
       else{
         alert("Error: status code is " + request.status);}
  }
}

 function checkAndSubmit(whatVote,radioName,formDiv,resultDiv,itemHeight) //
{  for(i=0;i<radioName.length;i++ )
  {
  if (radioName[i].checked==true)
  {
  callServer(radioName[i].value,whatVote,formDiv,resultDiv,itemHeight);
  return true;
  }
     }
  alert("您还没有做出选择就提交了!");
}

 

 

VoteAndView.asp

 

 

<!-- #Include Virtual = "/_system/on_pagestart.asp" -->

<%
DIM sql_D,strR,voted,whatVote,Rss,itemHeight,voteSum,cooName
  voted=""
  Response.expires=0
  whatVote=request("whatVote")
  itemHeight=cstr(request("itemHeight"))
  cooName="votedc"+request("whatVote")
  if request.Cookies(cooName)="true"  and Request("strValue")<>"" then
  voted="1"
  end if
  if Request("strValue")<>"" and request.Cookies(cooName)="" then
 sql_D="update Ballot_Content set Ballotcounts=Ballotcounts+1 where BallotID='"&whatVote&"' and "
 sql_D=sql_D+" Ballotcontent='"&Request("strValue")&"' "
 System.DB.Execute(sql_D)
 Response.Cookies(cooName)="true"
   Response.Cookies(cooName).expires=date+1
 end if
  sql="select SUM(Ballotcounts) as voteSum from ballot_content where ballotid='"&whatVote&"'"
  Set Rss = System.DB.Recordset(sql)
  voteSum=Rss("voteSum")
  Rss.Close
  
     strR=strR+"<table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0"">"
  dim counts,votePercent,tdWidth
  counts=0
     sql="select Ballotcontent,Ballotcounts from ballot_content where ballotid='"&whatVote&"'"
   Set Rss = System.DB.Recordset(sql)
  IF Not Rss.EOF Then
  Do While Not Rss.EOF
  counts=counts+1
     strR=strR+"<tr><td height="+itemHeight+" width=""45%"">"+Rss("Ballotcontent")+"</td>"
    votePercent=cstr(round(Rss("Ballotcounts")/voteSum*100,1))
    tdWidth=votePercent+"%"
    strR=strR+"<td width=""55%""><table width=""100%"" border=""0"" cellspacing=""0"" cellpadding=""0""><tr><td width=""55%""><table width="+tdWidth+" border=""0"" bgcolor=""#818975"" height=""10"" cellspacing=""0"" cellpadding=""0""><tr><td style=""font-size:9px;color:#000"">"+cstr(Rss("Ballotcounts"))+"</td></tr></table></td><td>"+votePercent+"%</td></tr></table></td></tr>"
  Rss.MoveNext
  LOOP
  End If
  Rss.Close
  Set Rss=Nothing
 if Request("strValue")="" then
 dim strReturn,formDiv,resultDiv
 formDiv="'"+request("formDiv")+"'"
 resultDiv="'"+request("resultDiv")+"'" 
 strReturn="document.getElementById("+formDiv+").style.display="""";document.getElementById("+resultDiv+").style.display=""none"";"
 strR=strR+"<tr><td align=""center"">一共"+cstr(voteSum)+"票</td><td οnclick="+strReturn+" style=""cursor:hand;text-decoration:underline"">投票</td></tr>"
 else
 strR=strR+"<tr><td align=""center"">一共"+cstr(voteSum)+"票</td><td>投票已完成</td></tr>"
 end if
 strR=strR+"</table>|"+voted
 response.Write strR
%>

 

 

<input type="button" name="Submit1" class="input50px" value="投票" οnclick="checkAndSubmit(5,this.form.votename3,'voteAform','voteA',29);"  />
     <input type="button" name="Submit2" value="查看" class="input50px" οnclick="callServer('',5,'voteAform','voteA',29);" />


FORM表单,只用传几个参数就可以,很好用,效果也很好,如果有帮助,自己看吧
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值