繁体   English   中英

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

[英]ajax php javascript php page open, parameters not being passed

我无法将一些值传递给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上的样本,但我是新的,仍在学习和好奇为什么我的代码现在不工作。

感谢所有帮助!!

请享用

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

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

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

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

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

在此输入图像描述

希望能帮助到你!。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM