weixin_33682790 2017-04-29 07:42 采纳率: 0%
浏览 16

Ajax投票实施

just quick issue i';m having trying to implement a simple Ajax poll into my web application. Thought it'd be a rather simplistic process; but after hours of searching and implementing; I can't tell if I'm doing something wrong on my end or it just isn't working. Followed alongside the w3 schools tutorial just to try and get something functioning; but after selecting the poll option; nothing is updated or changed alike the example suggests.

Html document

<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 file:

<?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>

Text file contents:

0||0 

Thanks for any help and feedback. Like i said; a simple solution was assumed but i'm really struggling to figure out why it's not functioning

  • 写回答

1条回答 默认 最新

  • weixin_33691598 2017-04-29 08:20
    关注

    Not sure why your code was failing but the following might be of use. The array structure you were employing seemed, to me, a little cumbersome - json would seem like a better option which is why I chose the method below. Hope it helps...

    <?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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分