我正在尝试在我的web应用程序中实现一个简单的Ajax轮询。我认为这是一个相当简单的过程;但是经过几个小时的搜索和实现,我无法判断自己到底是做错了什么,还是做不到。与w3学校教程并驾齐驱,只是为了发挥作用;但在选择投票选项之后,没有任何内容被更新或更改,示例建议。
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 (this.readyState==4 && this.status==200) {
document.getElementById("poll").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","poll_vote.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="poll">
<h3>Who do you want to vote for...</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 谢谢您的帮助和反馈。就像我说的,假设有一个简单的解决方案,但我真的很难弄清楚为什么它不能正常工作。
发布于 2017-04-29 08:20:25
不知道为什么您的代码失败,但以下可能有用。在我看来,您使用的数组结构有点麻烦- json似乎是一个更好的选择,这就是为什么我选择下面的方法。希望能帮上忙..。
<?php
if( isset( $_GET['vote'] ) ){
/* for testing - file in same directory as script */
$filename=__DIR__ . '\\poll.txt';
/* If the file exists, read it otherwise create empty object to store vote */
$contents=file_exists( $filename ) ? json_decode( file_get_contents( $filename ) ) : (object)array('yes'=>0,'no'=>0);
/* get the value of the vote cast */
$vote=intval( $_GET['vote'] );
/* increment the respective item */
switch( $vote ){
case 0:$contents->yes++; break;
case 1:$contents->no++; break;
}
/* write the data back to the text file */
file_put_contents( $filename, json_encode( $contents ) );
/* use the values from the json data */
$yes=$contents->yes;
$no=$contents->no;
/* use an object to aid caluclation of percentages ( makes easier notation imo )*/
$results=new StdClass;
$results->yes=100 * round( $yes /( $no + $yes ),2 );
$results->no=100 * round( $no /( $no + $yes ),2 );
/* structure the output response */
$response="
<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img src='poll.gif' width='{$results->yes}' height='20'> {$results->yes}%
</td>
</tr>
<tr>
<td>No:</td>
<td>
<img src='poll.gif' width='{$results->no}' height='20'> {$results->no}%
</td>
</tr>
</table>";
/* send the data back to the ajax callback function */
clearstatcache();
exit( $response );
}
?>
<!doctype html>
<html>
<head>
<title>Ajax Poll</title>
<script>
function getVote(int) {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
} else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("poll").innerHTML=this.responseText;
}
}
/* you will need to edit the url if you use a separate php script */
xmlhttp.open("GET","?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="poll">
<h3>Who do you want to vote for...</h3>
<form>
<!--
curious to know why "yes" has a value of 0 and "no" has a value of 1 - seems
kind of back to front...
-->
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>https://stackoverflow.com/questions/43693120
复制相似问题