文章来源:
100素材网
更新时间:
2014-08-11 14:04:30
php投票代码 php ajax投票 php投票网站源码 php 简单投票 投票程序 php
下面是通过ajax和php无刷新实现的投票代码
调查结果显示图:

客户端html文件
服务端php文件
它是存储这样的:
调查结果显示图:

客户端html文件
<html>
<head>
<script>
function getVote(int) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
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">
<h3>Do you like PHP and AJAX so far?</h3>
Yes:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)">
<br>No:
<input type="radio" name="vote" value="1" onclick="getVote(this.value)">
</div>
</body>
</html>
服务端php文件
<?php
$vote = $_REQUEST['vote'];
//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);
//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0) {
$yes = $yes + 1;
}
if ($vote == 1) {
$no = $no + 1;
}
//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>投票结果:</h2>
<table>
<tr>
<td>喜欢:</td>
<td>
<img src="http://www.100sucai.com/img/upload/poll.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>不喜欢:</td>
<td>
<img src="http://www.100sucai.com/img/upload/poll.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
文本文件(poll_result.txt)是我们存储的数据调查。它是存储这样的:
0 | | 0
除非特别声明,此稿为原稿转载请注明原文链接
原文地址:http://www.100sucai.com/code/1272.html
浏览次数次
上一篇文章: 实现用户成功登录Session传值的php实例代码
下一篇文章: php获取网站Alexa值排名的代码


推荐阅读:
