我是新手,我想让自己熟悉w3schools,并教自己一些奇怪的代码。到目前为止,我正在使用PHP AJAX Poll(http://www.w3schools.com/php/php_ajax_poll.asp),并且我了解其中的大部分内容。然后,我试图添加更多的值,尽管当我单击民意测验答案之一时,它将显示“一票”,但是当我重新加载页面时,答案就没有了(它将回到零%)。我将如何添加额外的投票答案?

干杯。

这是我在.html中的代码

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>LameDesign</title>
<script src="../../Scripts/swfobject_modified.js" type="text/javascript"></script>
<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>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="960" height="540" id="FLVPlayer">
  <param name="movie" value="FLVPlayer_Progressive.swf" />
  <param name="quality" value="high">
  <param name="wmode" value="opaque">
  <param name="scale" value="noscale">
  <param name="salign" value="lt">
  <param name="FlashVars" value="&amp;MM_ComponentVersion=1&amp;skinName=Clear_Skin_2&amp;streamName=../../backgroundwhite_14&amp;autoPlay=true&amp;autoRewind=false" />
  <param name="swfversion" value="8,0,0,0">
  <!-- This param tag prompts users with Flash Player 6.0 r65 and higher to download the latest version of Flash Player. Delete it if you don’t want users to see the prompt. -->
  <param name="expressinstall" value="../../Scripts/expressInstall.swf">
  <!-- Next object tag is for non-IE browsers. So hide it from IE using IECC. -->
  <!--[if !IE]>-->
  <object type="application/x-shockwave-flash" data="FLVPlayer_Progressive.swf" width="960" height="540">
    <!--<![endif]-->
    <param name="quality" value="high">
    <param name="wmode" value="opaque">
    <param name="scale" value="noscale">
    <param name="salign" value="lt">
    <param name="FlashVars" value="&amp;MM_ComponentVersion=1&amp;skinName=Clear_Skin_2&amp;streamName=../../backgroundwhite_14&amp;autoPlay=true&amp;autoRewind=false" />
    <param name="swfversion" value="8,0,0,0">
    <param name="expressinstall" value="../../Scripts/expressInstall.swf">
    <!-- The browser displays the following alternative content for users with Flash Player 6.0 and older. -->
    <div>
      <h4>Content on this page requires a newer version of Adobe Flash Player.</h4>
      <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p>
    </div>
    <!--[if !IE]>-->
  </object>
  <!--<![endif]-->
</object>
<script type="text/javascript">
swfobject.registerObject("FLVPlayer");
</script>

<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)">
<br>Maybe:
<input type="radio" name="vote" value="2" 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];
$maybe = $array[2];

if ($vote == 0) {
  $yes = $yes + 1;
}
if ($vote == 1) {
  $no = $no + 1;
}
if ($vote == 2) {
  $maybe = $maybe + 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+$maybe),3)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes+$maybe),3)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes+$maybe),3)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes+$maybe),3)); ?>%
</td>
</tr>
<tr>
<td>Maybe:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($maybe/($no+$yes+$maybe),3)); ?>'
height='20'>
<?php echo(100*round($maybe/($no+$yes+$maybe),3)); ?>%
</td>
</tr>
</table>

最佳答案

在这里,我添加了第三个选项“也许”检查我的代码

poll_vote.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];
$maybe = $array[2];

if ($vote == 0) {
  $yes = $yes + 1;
}
if ($vote == 1) {
  $no = $no + 1;
}
if($vote == 2){
     $maybe = $maybe + 1;
}

//insert votes to txt file
$insertvote = $yes."||".$no."||".$maybe;;
$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+$maybe),2)); ?>'
height='20'>
<?php echo(100*round($yes/($no+$yes+$maybe),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($no+$yes+$maybe),2)); ?>'
height='20'>
<?php echo(100*round($no/($no+$yes+$maybe),2)); ?>%
</td>
</tr>
<tr>
<td>May Be:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($maybe/($no+$yes+$maybe),2)); ?>'
height='20'>
<?php echo(100*round($maybe/($no+$yes+$maybe),2)); ?>%
</td>
</tr>
</table>


Index.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)">
<br>May be:
<input type="radio" name="vote" value="2" onclick="getVote(this.value)">
</form>
</div>

</body>
</html>

关于javascript - PHP AJAX Poll轻松将代码从2个变量答案更改为2个以上答案?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28089692/

10-16 18:19