</pre><pre name="code" class="html"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>投票</title>
<script>
function OnRadioClick(var11){
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.status == 200 && xmlhttp.readyState == 4){
poolDiv = document.getElementById("poll");
poolDiv.innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","投票.php?value=" + var11, true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="poll">
<form>
<span>喜欢AJAX么?</span>
喜欢:<input type="radio" value="0" οnclick="OnRadioClick(this.value)">
不喜欢:<input type="radio" value="1" οnclick="OnRadioClick(this.value)">
</form>
</div>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<?php
/**
* Created by PhpStorm.
* User: liyanq
* Date: 16/7/15
* Time: 21:07
*/
$result = htmlspecialchars($_GET["value"]);
$fileName = "poll_result.text";
$content = file($fileName);
$yesCount = explode("||",$content[0])[0];
$noCount = explode("||",$content[0])[1];
if($result == 0){
$noCount+=1;
}else if($result == 1) {
$yesCount+=1;
}
$insertContent = $yesCount . "||" . $noCount;
//这里有个文的权限问题,只有设置了everyone的读写权限后,就没有问题了.
//但没搞明白,自己的用户有读写权限,却还抱错....
$file = fopen($fileName,"w");
fputs($file,$insertContent);
fclose($file);
?>
<t2>结果</t2>
<table>
<tr>
<td>是:</td>
<td><span style="display: inline-block; background: #3bff12; width:<?php
echo (100*round($yesCount/($yesCount+$noCount),2));
?>px;height: 20px;">
</span>
<?php echo (100*round($yesCount/($yesCount+$noCount),2)); ?>
</td>
</tr>
<tr>
<td>否:</td>
<td><span style="display: inline-block;background: red;width: <?php
echo (100*round($noCount/($yesCount + $noCount),2));?>px;height: 20px;">
</span>
<?php echo (100*round($noCount/($yesCount + $noCount),2));?>
</td>
</tr>
</table>
</body>
</html>
注意:用php结果处理span的宽度时候,一定要加上px单位,否则无效。感谢haoc的帮助。