【问题标题】:Display results of AJAX / PHP poll on initial page load + after voting在初始页面加载 + 投票后显示 AJAX/PHP 投票结果
【发布时间】:2014-04-03 16:59:04
【问题描述】:

使用 w3school 的 PHP / AJAX 投票示例,我很好奇如何对其进行修改,以便在用户选择投票选项后除了更新之外,还可以在初始页面加载时显示投票结果。带有代码的页面在这里:http://www.w3schools.com/php/php_ajax_poll.asp。我也在下面复制了它:

HTML 文件:

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

<div id="poll">
<h3>Do you like PHP and AJAX so far?</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 文件:

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

文本文件:

0||0

【问题讨论】:

    标签: php html ajax


    【解决方案1】:

    HTML(编辑行):

    <body onload="readVote();">
    

    JS(添加到代码中):

    function readVote()
    {
    if (window.XMLHttpRequest)
      {
      xmlhttp = new XMLHttpRequest();
      }
    else
      {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange = function()
      {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
        document.getElementById("poll").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","poll_vote_read.php",true);
    xmlhttp.send();
    }
    

    PHP(新建文件,名称为“poll_vote_read.php”):

    //get content of textfile
    $filename = "poll_result.txt";
    $content = file($filename);
    
    $array = explode("||", $content[0]);
    $yes = $array[0];
    $no = $array[1];
    
    ?>
    
    <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>
    

    我认为这可行,但我没有测试它,如果可行,请投票!

    【讨论】:

      猜你喜欢
      • 2022-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多