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表单,只用传几个参数就可以,很好用,效果也很好,如果有帮助,自己看吧