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);
}
?>