V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
porwyn
V2EX  ›  问与答

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

  •  
  •   porwyn · 2017-12-12 11:02:04 +08:00 · 898 次点击
    这是一个创建于 2349 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    这是最终的期望效果

    共有三个文件,如下:

    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

    2 条回复    2017-12-12 11:15:19 +08:00
    whypool
        1
    whypool  
       2017-12-12 11:08:08 +08:00   ❤️ 1
    既然用了 xhr,还用啥 from
    from 表单提交,默认 method 为 get,默认 action 为当前 url,你点了之后执行的是 from 提交
    porwyn
        2
    porwyn  
    OP
       2017-12-12 11:15:19 +08:00
    @whypool 菜鸟不太懂,可以告诉下错误的地方该改成什么吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2167 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 02:43 · PVG 10:43 · LAX 19:43 · JFK 22:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.