如何显示投票的响应者总数



我有一个使用 PHP 和 Ajax 进行的民意调查,民意调查是一个是/否问题,我的代码只显示是和否的百分比。最大的问题是:我如何显示响应者总数。在下面,ii将显示我使用的代码:

<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 (this.readyState==4 && this.status==200) {
      document.getElementById("poll").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","poll_app/poll_vote.php?vote="+int,true);
  xmlhttp.send();
}
</script>
<h3 align="center">Are you satisfied with our services?</h3><br>
<form id="pollform">
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)">
</form>
</div>

该 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>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll1.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll2.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>

您的文本文件中有两个总答案。您可以简单地在下面显示带有百分比 (%( 的单个响应和总响应。就像下面的html代码一样,你可以写一些东西:

<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src="poll1.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
(<?php echo $yes;  ?> Vote) </td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll2.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
  : (<?php echo $no;  ?> Vote)   </td>
</tr>
<tr> <?php echo $yes+$no; ?> total responses. </tr>
</table>

最新更新