首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax Poll实现

Ajax Poll实现
EN

Stack Overflow用户
提问于 2017-04-29 07:42:48
回答 1查看 53关注 0票数 0

我正在尝试在我的web应用程序中实现一个简单的Ajax轮询。我认为这是一个相当简单的过程;但是经过几个小时的搜索和实现,我无法判断自己到底是做错了什么,还是做不到。与w3学校教程并驾齐驱,只是为了发挥作用;但在选择投票选项之后,没有任何内容被更新或更改,示例建议。

Html文档

代码语言:javascript
复制
<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 (this.readyState==4 && this.status==200) {
      document.getElementById("poll").innerHTML=this.responseText;
    }
  }
  xmlhttp.open("GET","poll_vote.php?vote="+int,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<div id="poll">
<h3>Who do you want to vote for...</h3>
<form>
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>

</body>
</html>

PHP文件:

代码语言:javascript
复制
<?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="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>No:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>

文本文件内容:

代码语言:javascript
复制
0||0 

谢谢您的帮助和反馈。就像我说的,假设有一个简单的解决方案,但我真的很难弄清楚为什么它不能正常工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-29 08:20:25

不知道为什么您的代码失败,但以下可能有用。在我看来,您使用的数组结构有点麻烦- json似乎是一个更好的选择,这就是为什么我选择下面的方法。希望能帮上忙..。

代码语言:javascript
复制
<?php
    if( isset( $_GET['vote'] ) ){

        /* for testing - file in same directory as script */
        $filename=__DIR__ . '\\poll.txt';

        /* If the file exists, read it otherwise create empty object to store vote */
        $contents=file_exists( $filename ) ? json_decode( file_get_contents( $filename ) ) : (object)array('yes'=>0,'no'=>0);

        /* get the value of the vote cast */
        $vote=intval( $_GET['vote'] );

        /* increment the respective item */
        switch( $vote ){
            case 0:$contents->yes++; break;
            case 1:$contents->no++; break;
        }

        /* write the data back to the text file */
        file_put_contents( $filename, json_encode( $contents ) );

        /* use the values from the json data */
        $yes=$contents->yes;
        $no=$contents->no;


        /* use an object to aid caluclation of percentages ( makes easier notation imo )*/
        $results=new StdClass;
        $results->yes=100 * round( $yes /( $no + $yes ),2 );
        $results->no=100 * round( $no /( $no + $yes ),2 );


        /* structure the output response */
        $response="
            <h2>Result:</h2>
            <table>
                <tr>
                    <td>Yes:</td>
                    <td>
                        <img src='poll.gif' width='{$results->yes}' height='20'> {$results->yes}%
                    </td>
                </tr>
                <tr>
                    <td>No:</td>
                    <td>
                        <img src='poll.gif' width='{$results->no}' height='20'> {$results->no}%
                    </td>
                </tr>
            </table>";


        /* send the data back to the ajax callback function */
        clearstatcache();
        exit( $response );
    }
?>
<!doctype html>
<html>
    <head>
        <title>Ajax Poll</title>
        <script>
            function getVote(int) {
              if (window.XMLHttpRequest) {
                xmlhttp=new XMLHttpRequest();
              } else {
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
              xmlhttp.onreadystatechange=function() {
                if (this.readyState==4 && this.status==200) {
                  document.getElementById("poll").innerHTML=this.responseText;
                }
              }

              /* you will need to edit the url if you use a separate php script */
              xmlhttp.open("GET","?vote="+int,true);
              xmlhttp.send();
            }
        </script>
    </head>
    <body>
        <div id="poll">
            <h3>Who do you want to vote for...</h3>
            <form>
                <!--
                    curious to know why "yes" has a value of 0 and "no" has a value of 1 - seems
                    kind of back to front...
                -->
                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>
    </body>
</html>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43693120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档