我试图将javascript循环的每次迭代后正在更新的值传递给返回枯萎的php脚本,否则循环将继续。我在循环的第一次迭代后得到了这个错误
ERR: 8) (Undefined index: player2name) (loaction of php file on host) (Line:� 26)
这是有问题的javascript函数
function startgame()
{
document.JForm.delay.disabled = true;
document.JForm.p1name.disabled = true;
document.JForm.p2name.disabled = true;
document.JForm.numgames.disabled = true;
document.JForm.totalpoints.disabled = true;
//Create the XMLHttpRequest Object
var xmlhttp;
// error handling
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false;
}
}
http = xmlhttp;
var rolling = 1;
var url = "program.php?player1name=";
var player1name = document.JForm.p1name.innerHTML;
var player2name = document.JForm.p2name.innerHTML;
var playtopoints = parseInt(document.JForm.totalpoints.value);
var delay = parseInt(document.JForm.delay.value);
var numgames = parseInt(document.JForm.numgames.value);
var gamesplayed = parseInt(document.JForm.gamesplayed.value);
var p1turn = parseInt(document.JForm.p1turn.value);
var p2turn = parseInt(document.JForm.p2turn.value);
var p1total = parseInt(document.JForm.p1total.value);
var p2total = parseInt(document.JForm.p2total.value);
var sdata = url + player1name +"&?player2name=" + player2name + "&?playtopoints=" + playtopoints + "&?delay=" + delay + "&?numgames=" + numgames + "&?gamesplayed=" + gamesplayed + "&?p1turn=" + p1turn + "&?p2turn=" + p2turn + "&?p1total=" + p1total + "&?p2total=" + p2total;
for (var i = 0; i<numgames; i++){
http.open("POST", url + sdata, true);
rolling = "<?php echo $roll; ?>";
document.JForm.p1name.innerHTML = rolling;
if (rolling){
roll();
}
else{
//update player stats there was a winner
alert("not rolling");
stop();
if (currentplayer == "P1"){
currentplayer = "P2";
}
else{
currentplayer = "P1";
}
}
}
// register with server what function to return data
http.onreadystatechange = handleHttpResponse;
// send request to server
http.send(null);
}
这里是php代码
<?php
error_reporting(E_ALL); // Enable all error checking
$return = set_error_handler("MyError");
// Error handler function, called when PHP runtime error detected
function MyError($errno,$errstr,$errfile,$errline) {
print "(ERR: $errno) ($errstr) ($errfile) (Line:Ê $errline) <br>";
return true;
}
// End of PHP error handling code
$p1name=$_GET['player1name']; // algorithm player 1 is using
$p2name = $_GET['player2name'];
$playtopoints = $_GET['playtopoints'];
$delay = $_GET['delay'];
$numgames = $_GET['numgames'];
$gamesplayed=$_GET['gamesplayed']; // number of games played from JavaScript
$p1turn = $_GET['p1turn'];
$p2turn = $_GET['p2turn'];
$p1total = $_GET['p1total'];
$p2total = $_GET['p2total'];
//if statement to find who the current player is
if ($numgames % 2 == 0){
//number is even
$currentpoints = $p1turn;
$currenttotal = $p1total;
$optotal = $p2total;
}
else{
//number is odd
$currentpoints = $p2turn;
$currenttotal = $p2total;
$optotal = $p1total;
}
sleep($delay);
$rolling = algorithm1($playtopoints, $p1turn, $p2turn, $p1total, $p2total);
print $rolling;
exit(); // return to browser
function algorithm1($wintotal, $p1t, $p2t, $p1total, $p2total) {
if (($p1t >= ($wintotal / 2) )||($p2t >= ($wintotal / 2))){
$continue = 0;
}
else if(($p1t <= 20)||($p2t <= 20)){
$continue = 1;
}
return($continue);
}
?>
答案 0 :(得分:1)
JavaScript中存在多个错误。
问题1:以下行?
之后的&
var sdata = url + player1name +"&?player2name=" + player2name + "&?playtopoints=" + playtopoints + "&?delay=" + delay + "&?numgames=" + numgames + "&?gamesplayed=" + gamesplayed + "&?p1turn=" + p1turn + "&?p2turn=" + p2turn + "&?p1total=" + p1total + "&?p2total=" + p2total;
问题2:两次发送url
http.open("POST", url + sdata, true);
sdata
已包含url
个变量。删除重复的url
变量。
问题3:混合POST
和GET
http.open("POST", url + sdata, true);
您通过POST
方法发送了HTTP请求,但在PHP方面,您通过GET
收到了数据。更改任意一方,但我建议使用POST
,即在PHP中将$_GET
更改为$_POST
。
旁注:建议使用类似jQuery的库来简化HTTP请求代码。
答案 1 :(得分:0)
我可以看到一些额外的'?'在
var sdata = url + player1name +"&?player2name=" + player2name + "&?playtopoints=" + playtopoints + "&?delay=" + delay + "&?numgames=" + numgames + "&?gamesplayed=" + gamesplayed + "&?p1turn=" + p1turn + "&?p2turn=" + p2turn + "&?p1total=" + p1total + "&?p2total=" + p2total;
也许你可以使用
var sdata = url + "?player1name=" + player1name +"&player2name=" + player2name + "&playtopoints=" + playtopoints + "&delay=" + delay + "&numgames=" + numgames + "&gamesplayed=" + gamesplayed + "&p1turn=" + p1turn + "&p2turn=" + p2turn + "&p1total=" + p1total + "&p2total=" + p2total;
我在你的网址中添加了一些额外的'player1name'以及剥去额外的'?'