vote.php
poll_vote.php
<html>
<head>
<meta charset="utf-8">
<title>vote</title>
<script>
function getVote(int) {
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("poll").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="poll">
<form>
菊花侠:
<input type="radio" name="vote" value="0" οnclick="getVote(this.value)">
<br>桃花怪:
<input type="radio" name="vote" value="1" οnclick="getVote(this.value)">
</form>
</div>
</body>
</html>
poll_vote.php
<?php
header("Content-type:text/html;charset=utf-8");
$vote = htmlspecialchars($_REQUEST['vote']);
// 获取文件中存储的数据
$fileDir = "poll_result.txt";
$content = file_get_contents($fileDir);
// 将数据分割到数组中
$array = explode("||", $content);
$yes = $array[0];
$no = $array[1];
if ($vote == 0)
{
$yes+=1;
}
if ($vote == 1)
{
$no+=1;
}
// 插入投票数据
$insertvote = $yes."||".$no;
$fp = fopen($fileDir,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>投票结果:</h2>
<table>
<tr>
<td>菊花侠:</td>
<td>
<span style="display: inline-block; background-color:green;
width:<?php echo(100*round($yes/($no+$yes),2)); ?>px;
height:20px;" ></span>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>桃花怪:</td>
<td>
<span style="display: inline-block; background-color:red;
width:<?php echo(100*round($no/($no+$yes),2)); ?>px;
height:20px;"></span>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>