Ajax Simple voting example in JavaScript (JS version)

Source: Internet
Author: User
Tags setcookie

Note: I simply use cookies to prevent repetitive voting, and I'm sure I can cheat, and the safest thing is to sign up for a vote.

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<meta http-equiv= "keywords" content= "flash,flex,air,ajax,html5,silverlight"/>
<title> Events Program Voting </title>
<style>
body{
margin:0px;
font-size:12px;
}

#header {
padding-top:5px;
padding-left:10px;
}
. pbg{
Background-color: #EEE;
}

. pbr{
height:8px;
}

li{
List-style-type:none;
}
HR {
border:0px;
Color: #CCCCCC;
Background-color: #CDCDCD;
height:1px;
width:100%;
}

H2 {
Color: #CC0000;
Text-align:center;
}
h3 {
Color: #999999;
Text-align:left;
font-weight:300;
padding:5px;
margin-top:5px;
}

#main {
padding-left:10px;
padding-bottom:10px;
}

#detail {
padding:10px;
}

#detail Div {
padding-left:5px;
}
p {
line-height:14px;
}
A
Color: #CC0000;
Background-color: #FFFFFF;
Text-decoration:none;
}
a:hover {
Color: #cc0000;
Background-color: #FFFFFF;
Text-decoration:underline;
}
#footer {
Text-align:center;
}
. info{
padding-left:6px;
position:relative;
}
#msg {
padding-left:30px;
Color: #990000;
}

</style>
<script type= "Text/javascript" >

VAR Ajax;
var msg;
var voting;

function init ()
{
msg = document.getElementById ("msg");
Voting = false;
Ajaxsendrequest ("Do.php?n=-1", onupdatedata);
}

function Createajax () {
if (window. ActiveXObject) {
try{
return new ActiveXObject ("MSXML2.") XMLHTTP ");
}
catch (e) {
try{
return new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (E2) {
return null;
}
}
}//www.111cn.net
else if (window. XMLHttpRequest) {
return to new XMLHttpRequest;
}
else{
return null;
}
}

function Onupdatedata () {
if (ajax.readystate==4) {
if (ajax.status==200) {
var text = Ajax.responsetext;
UpdateUI (text);
}
else{
Alert ("Server handling Error");
}
}
}

function Ajaxsendrequest (URI, handler) {
Ajax = Createajax ();
if (!ajax) {
Alert (' Using an incompatible XMLHttpRequest browser ');
return 0;
}

Ajax.onreadystatechange = handler;
Ajax.open ("Get", uri,true);
Ajax.send (NULL);
}

function Checkform () {
Voting = true;
var options = document.getelementsbyname ("option");
var voted = GetCookie ("voted")? GetCookie ("voted"): false;
var num =-1;
for (var i = 0; i < options.length; i++)
{
var option = Options[i];
if (option.checked)
{
num = Option.value;
}
}
if (num = = 1)
{
msg.innerhtml = "Please click to choose!" ";
Clearinfo ();
}
Else
{
if (!voted)
{
msg.innerhtml = "";
var today = new Date ();
url = "do.php?n=" + num + "&time=" + today.gettime ();
Ajaxsendrequest (URL, onupdatedata);
}
Else
{
msg.innerhtml = "Please do not repeat the vote!" ";
Clearinfo ();
}
}
return false;
}

Function UpdateUI (text)
{
 var counts = Text.split (":");
 var sum = getsum (counts);
 var EMS = document.getElementsByTagName ("em");
 for (var i = 0; i < counts.length i++)
 {
  var count = counts[i];
  v AR pb = document.getElementById ("pb_" + i);
  var perscent = isNaN (count)? "": (Count/sum *). ToFixed (2) + "%";
  pb.style.width = perscent;
  var percenttd = document.getElementById ("Percent_" + i.tostring ());
  percenttd.innerhtml =  "(" + perscent + ")";
  var countem = ems[i];
  countem.style.color = Pb.style.backgroundColor;
  countem.innerhtml =  count.tostring ();
 }
 if (voting)
 {
  setcookie ("voted", true);
  msg.innerhtml = "The vote was successful!" Thank you for your participation! ";
  clearinfo ();
 }
}

function Getsum (counts)
{
var sum = 0;
for (var j = 0; J < Counts.length; J + +)
{
var count = parseint (Counts[j]);
sum + = count;
}
return sum;
}

function Clearinfo ()
{
settimeout (function () {
msg.innerhtml = "";
},2000);
}

function Setcookie (name,value)
{
var days = 30;
var exp = new Date ();
Exp.settime (Exp.gettime () + days*24*60*60*1000);
Document.cookie = name + "=" + Escape (value) + "expires=" + exp.togmtstring ();
}

function GetCookie (name)
{
var arr = Document.cookie.match (New RegExp ("(^|)" +name+ "= ([^;] *)(;|$)"));
if (arr!= null) return unescape (arr[2)); return null;
}

function ClearCookie (name)
{
var exp = new Date ();
Exp.settime (Exp.gettime ()-1);
var cval=getcookie (name);
if (cval!=null) document.cookie= name + "=" +cval+ "; expires=" +exp.togmtstring ();
}


</script>

<body onload= "Init ();" >
<div id= "Header" >
</div>
<div id= "Main" >
<table width= "100%" cellpadding= "0" cellspacing= "0" >
<tr>
&LT;TD width= "rowspan=" 2 "><input type=" Radio "name=" option "id=" option_0 "value=" 0 "/></td>
&LT;TD colspan= "4" ><label for= "Option_0" >1. 10 Poly Chengdu Tour (2 days) </label></td>
</tr>
<tr>
<td>
<div class= "PBG" >
<div id= "Pb_0" class= "PBR" style= "Background-color": #990000; ></div>
</div>
</td>
&LT;TD width= "></td>"
&LT;TD id= "Percent_0" width= "></td>"
&LT;TD width= "><em></em></td>"
</tr>
<tr>
&LT;TD rowspan= "2" ><input type= "Radio" name= "option" id= "option_1" value= "0"/></td>
&LT;TD colspan= "4" ><label for= "option_1" >2. 10 Dujiangyan Travel (2 days) </label></td>
</tr>
<tr>
<td>
<div class= "PBG" >
<div id= "pb_1" class= "PBR" style= "Background-color": #000099; ></div>
</div>
</td>
<td></td>
&LT;TD id= "Percent_1" ></td>
<td><em></em></td>
</tr>
<tr>
&LT;TD rowspan= "2" ><input type= "Radio" name= "option" id= "option_2" value= "0"/></td>
<td><label for= "Option_2" >3. 10 Poly Wawushan Tour (3 days) </label></td>
</tr>
<tr>
<td>
<div class= "PBG" >
<div id= "pb_2" class= "PBR" style= "Background-color": #009900; ></div>
</div>
</td>
<td></td>
&LT;TD id= "Percent_2" ></td>
<td><em></em></td>
</tr>
<tr>
&LT;TD rowspan= "2" ><input type= "Radio" name= "option" id= "option_3" value= "0"/></td>
<td><label for= "Option_3" >4. 10 Poly Wan County Tour (3 days) </label></td>
</tr>
<tr>
<td>
<div class= "PBG" >
<div id= "Pb_3" class= "PBR" style= "Background-color": #999900; ></div>
</div>
</td>
<td></td>
&LT;TD id= "Percent_3" ></td>
<td><em></em></td>
</tr>
<tr>
&LT;TD rowspan= "2" ><input type= "Radio" name= "option" id= "Option_4" value= "0"/></td>
<td><label for= "Option_4" >5. 10 Poly-Wan County Journey to Nostalgia (3 days) </label></td>
</tr>
<tr>
<td>
<div class= "PBG" >
<div id= "Pb_4" class= "PBR" style= "Background-color": #009999; ></div>
</div>
</td>
<td></td>
&LT;TD id= "Percent_4" ></td>
<td><em></em></td>
</tr>
</table>
</div>
<div align= "center" >
<input type= "button" value= "I want to vote" onclick= "Checkform ();" />
</div>
<span id= "MSG" ></span>

</div>
<div id= "Footer" >
</div>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.