请教 PHP +AJAX+TXT 投票怎么可以多选?

2017-12-12 11:02:04 +08:00
 porwyn

现在的情况是每点完一个就跳转到结果了,想问下怎么可以多选。

这是最终的期望效果

共有三个文件,如下:

index.html

<html>
<head>
  <meta charset="utf-8">
  <script type="text/javascript">
    // 这里是 js 代码
    function getVote(int) {
      if (window.XMLHttpRequest) {
        // 创建 XMLHttpRequest 对象
        // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码
        xmlhttp = new XMLHttpRequest();
      } else {
        //IE6, IE5 浏览器执行的代码
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      // 监听响应
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState ==4 && xmlhttp.status == 200) {
          // 找到 id 为 poll 的控件
          document.getElementById('poll').innerHTML = xmlhttp.responseText;
        }
      }
      // 向 PHP 脚本传递主要参数 q
      xmlhttp.open("GET", "poll_vote.php?q=" + int, true);
      xmlhttp.send();
    }
  </script>
</head>
<body>
  
  <div id="poll">
    <form>
	<img src="http://www.ynpxrz.com/images/logo.gif" width="165" height="60" /><br>
      <font size="2" color="red">顶</font> 
	  <input type="radio" name="vote" value="0" onclick="getVote(this.value)"> 
	  <font size="2" color="black">踩</font> 
	  <input type="radio" name="vote" value="1" onclick="getVote(this.value)">
    </form>

    <form>
	<img src="http://www.ynpxrz.com/images/logo.gif" width="165" height="60" /><br>
      <font size="2" color="red">顶</font> 
	  <input type="radio" name="vote" value="2" onclick="getVote(this.value)"> 
	  <font size="2" color="black">踩</font> 
	  <input type="radio" name="vote" value="3" onclick="getVote(this.value)">
    </form>
  </div>
  
</body>
</html>

poll_vote.php

<?php
  // 接收参数 q
  $vote = htmlspecialchars($_REQUEST['q']);
  // 获取文件中存储的数据
  $filename = "poll_result.txt";
  $conn = file($filename);
  // 将数据分割到数组 (分行列出数值,COUNT 末尾。)
  $array = explode("||", $conn[0]);
  $yes = $array[0];
  $no = $array[1];
  $ye = $array[2];
  $n = $array[3];
  $count = $array[4];
  // VOTE 为数组编号
  if ($vote == 0) {
    $yes += 1;
    $count += 1;
  }
  if ($vote == 1) {
    $no += 1;
    $count += 1;
  }
  if ($vote == 2) {
    $ye += 1;
    $count += 1;
  }
  if ($vote == 3) {
    $n += 1;
    $count += 1;
  }
  // 将投票数据保存到文档 (分别设立数值)
  $insertvote = $yes . '||' . $no . '||' . $ye . '||' . $n . '||' . $count;
  $fp = fopen($filename, "w");
  fputs($fp, $insertvote);
  fclose($fp);
 ?>

 <table>
  
  <tr>
    <td><font size="1" color="red">红&nbsp;<?php echo 100 * round($yes / ($yes + $no), 4); ?>%</font></td>
    <td><span style="display: inline-block; background-color: red; width: <?php echo 50 * round($yes / ($yes + $no), 4);?>px; height: 10px;"></span></td>
    <td><font size="1" color="black">黑&nbsp;<?php echo 100 * round($no / ($yes + $no), 4); ?>%</font></td>
	<td><span style="display: inline-block; background-color: black; width: <?php echo 50 * round($no / ($yes + $no), 4);?>px; height: 10px;"></span></td>
  </tr>
  
  
  <tr>
    <td><font size="1" color="red">红&nbsp;<?php echo 100 * round($ye / ($ye + $n), 4); ?>%</font></td>
    <td><span style="display: inline-block; background-color: red; width: <?php echo 50 * round($ye / ($ye + $n), 4);?>px; height: 10px;"></span></td>
    <td><font size="1" color="black">黑&nbsp;<?php echo 100 * round($n / ($ye + $n), 4); ?>%</font></td>
	<td><span style="display: inline-block; background-color: black; width: <?php echo 50 * round($n / ($ye + $n), 4);?>px; height: 10px;"></span></td>
  </tr>
  
 </table>
 
 <h5><p><?php echo "已有 " . $count . " 人参与本次投票!"; ?></p></h5>

poll_result.txt

907 次点击
所在节点    问与答
2 条回复
whypool
2017-12-12 11:08:08 +08:00
既然用了 xhr,还用啥 from
from 表单提交,默认 method 为 get,默认 action 为当前 url,你点了之后执行的是 from 提交
porwyn
2017-12-12 11:15:19 +08:00
@whypool 菜鸟不太懂,可以告诉下错误的地方该改成什么吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/414022

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX