0
我有麻烦传递一些值到一个PHP页面,不知道什么是错的,只知道错误发生在xmlhttp.open。尝试了很多东西,无法弄清楚。ajax php JavaScript的页面打开,参数没有通过
继承人的代码: HTML:
<html>
<head>
<script>
function getVote(i, j) {
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?vote1="+i+"&vote2="+j,true);
xmlhttp.send();
//document.write(i + ' ' + j);
}
</script>
</head>
<body>
<div id="poll">
<h3>Are you a skier, or do you get stout on the board mon? Either way, lets see if shred...</h3>
<form>
What do you do?<br>
Ski:
<input type="radio" name="vote1" id="vote1" value="0" >
<br>Snowboard:
<input type="radio" name="vote1" id="vote1" value="1">
<br>Dream of getting rad:
<input type="radio" name="vote1" id="vote1" value="2">
<br>
Do you watch other people hit the jump first?<br>
Yes:
<input type="radio" name="vote2" id="vote2" value="0" onclick="var tmpora = document.getElementById('vote1').value; getVote(tmpora, this.value);">
<br>No:
<input type="radio" name="vote2" id="vote2" value="1" onclick="var tmpora = document.getElementById('vote1').value; getVote(tmpora, this.value);">
<br>
</form>
</div>
</body>
</html>
和PHP:
<?php
$vote1 = $_REQUEST['vote1'];
$vote2 = $_REQUEST['vote2'];
$filename = "poll_result.txt";
$content = file($filename);
$array = explode("||", $content[0]);
$ski = $array[0];
$board = $array[1];
$no = $array[2];
$radski = $array[3];
$radboard = $array[4];
if ($vote1 == 0) {
$ski = $ski + 1;
} else if ($vote1 == 1) {
echo "howdyyy";
$board = $board + 1;
} else if ($vote1 == 2) {
$no = $no + 1;
}
if ($vote2 == 0) {
if ($ski == 1) {
$radski = $radski + 1;
} else if ($board == 1) {
$radboard = $radboard + 1;
} else if ($no == 1) {
}
$no = $no + 1;
} else if ($vote2 == 1) {
}
$insertvote = $ski."||".$board"||".$no"||".$radski"||".$radboard;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Result:</h2>
<table>
<tr>
<td>Skiers:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($ski/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($ski/($ski+$board+$no),2)); ?>%
</td>
</tr>
<tr>
<td>Snowboarders:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($board/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($board/($ski+$board+$no),2)); ?>%
</td>
</tr>
<tr>
<td>Dreamers:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($no/($ski+$board+$no),2)); ?>%
</td>
</tr>
<tr>
<td>Rad Skiers:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($radski/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($radski/($ski+$board+$no),2)); ?>%
</td>
</tr>
<tr>
<td>Rad Snowboarders:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($radboard/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($radboard/($ski+$board+$no),2)); ?>%
</td>
</tr>
</table>
和txt文件,如:
0 || 0 || 0 || 0 || 0
这就像在w3schools上的样本,但我是新的,仍然在学习和好奇,为什么我的代码不工作现在。
感谢所有的帮助!
享受
你有没有考虑过使用类似jQuery或Zepto来处理ajax?对于单个请求来说,这是很多代码。如果你正在做很多这样的请求,那么你也可以使用经过充分测试并证明可以在各种浏览器中工作的库。 – naomik
有点儿,但即时通讯只是学习和wana去从头开始所有的过程。 – user1807880
您应该首先阅读[AJAX - 向服务器发送请求](http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp) – Pavlo