现在的情况是每点完一个就跳转到结果了,想问下怎么可以多选。
共有三个文件,如下:
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">红 <?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">黑 <?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">红 <?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">黑 <?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
1
whypool 2017-12-12 11:08:08 +08:00 1
既然用了 xhr,还用啥 from
from 表单提交,默认 method 为 get,默认 action 为当前 url,你点了之后执行的是 from 提交 |