ajax php javascript php页面打开,参数未被传递

时间:2014-03-06 08:36:43

标签: javascript php jquery html ajax

我无法将一些值传递给php页面,不知道什么是错的,只知道xmlhttp.open发生了错误。尝试了很多东西,无法弄明白。

继承人的代码: 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上的一个样本,但我是新的,仍在学习和好奇为什么我的代码现在不起作用。

感谢所有帮助的人!!

享受

1 个答案:

答案 0 :(得分:2)

好的,得到了​​这个bug。

使用以下代码替换PHP文件中第30行的代码:

$insertvote = $ski."||".$board."||".$no."||".$radski."||".$radboard;

您错过了.(附加)符号。

这是我得到的输出。忽略网络错误(我没有图像)

enter image description here

希望它有所帮助!