Php 如何显示投票的总响应人数?

Php 如何显示投票的总响应人数?,php,ajax,survey,Php,Ajax,Survey,我用PHP和Ajax做了一个调查,调查是一个肯定/否定的问题,我的代码只显示了肯定和否定的百分比。最大的问题是:我如何显示总响应者的数量。在下文中,Ii将显示我使用的代码: <script> function getVote(int) { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } el

我用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>

函数getVote(int){
if(window.XMLHttpRequest){
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}else{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“poll”).innerHTML=this.responseText;
}
}
open(“GET”、“poll\u app/poll\u vote.php?vote=“+int,true”);
xmlhttp.send();
}
您对我们的服务满意吗?
对:
否:
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>
$vote=$\u请求['vote'];
//获取文本文件的内容
$filename=“poll_result.txt”;
$content=文件($filename);
//将内容放入数组
$array=explode(“| |,$content[0]);
$yes=$array[0];
$no=$array[1];
如果($vote==0){
$yes=$yes+1;
}
如果($vote==1){
$no=$no+1;
}
//将投票插入txt文件
$insertvote=$yes.“| |”。$no;
$fp=fopen($filename,“w”);
FPUT($fp,$insertvote);
fclose($fp);
?>
结果:
对:
'
高度class='20'>
%
不:
'
高度class='20'>
%

您的文本文件中有两个完整答案。您可以简单地在下面显示带有百分比(%)的单个响应和总响应。就像下面的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>
结果:
对:
'
高度class='20'>
%
(表决)
不:
'
高度class='20'>
%
:(表决)
全部答复。

您有值($yes和$no),因此只需修改响应中的代码即可显示这些值$yes+$no=总投票数….+1@summit sharma,但下一个问题是,如何将用户IP的投票数限制为一个?@AndresConstantin在您的情况下,当您将投票保存在文本文件中时,只需像$yes一样添加用户的IP地址。"||" . $不"||" . $_服务器['REMOTE_ADDR'];但是,如果这不是一个很好的做法,在没有数据库的情况下处理所有这些问题,那么如果有这么多的用户将要投票,这可能会造成问题。谢谢