Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to pass values that are being updated after each iteration of a loop in javascript to a php script that returns wither or not the loop will continue. I am getting this error though after the first iteration of the loop

ERR: 8) (Undefined index: player2name) (loaction of php file on host) (Line:� 26) 

here is the javascript function that is having the issue

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

}

and here is the php code

    <?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);
    }
?>
share|improve this question
2  
You appear to be using AJAX to POST the data, but you're checking the $_GET array. The format of your data appears to be wrong too: you should use ? to start the query string and & to delimit the variables. You seem to be using &? as a delimiter. –  Mike W Oct 24 '13 at 2:24

2 Answers 2

There are multiple errors in the JavaScript .

Problem 1: Extra ? after & in the following line

var sdata = url + player1name +"&?player2name=" + player2name + "&?playtopoints=" + playtopoints + "&?delay=" + delay + "&?numgames=" + numgames + "&?gamesplayed=" + gamesplayed + "&?p1turn=" + p1turn + "&?p2turn=" + p2turn + "&?p1total=" + p1total + "&?p2total=" + p2total;

Problem 2: Sent url twice

http.open("POST", url + sdata, true);

which sdata already contains url variable. Remove the duplicated url variable.

Problem 3: Mix up POST and GET

http.open("POST", url + sdata, true);

You sent the HTTP request by POST method, but in PHP side you receive the data by GET. Change either side, but I recommend to use POST, i.e. change $_GET to $_POST in PHP.

sidenote: Suggested to use library like jQuery to simplify HTTP request codes.

share|improve this answer
    
I have made those changes but I am still getting the same error from php. –  user2793027 Oct 24 '13 at 3:01
    
You should debug the input in PHP via var_dump($_POST) or similar techniques. –  Raptor Oct 24 '13 at 3:02
    
After doing that I found that the array is actually empty can anyone suggest why that is? –  user2793027 Oct 24 '13 at 22:33
    
Double check your logic, add more checking codes... –  Raptor Oct 25 '13 at 6:08

I can see some extra '?' in

var sdata = url + player1name +"&?player2name=" + player2name + "&?playtopoints=" + playtopoints + "&?delay=" + delay + "&?numgames=" + numgames + "&?gamesplayed=" + gamesplayed + "&?p1turn=" + p1turn + "&?p2turn=" + p2turn + "&?p1total=" + p1total + "&?p2total=" + p2total;

maybe you can use

var sdata = url + "?player1name=" + player1name +"&player2name=" + player2name + "&playtopoints=" + playtopoints + "&delay=" + delay + "&numgames=" + numgames + "&gamesplayed=" + gamesplayed + "&p1turn=" + p1turn + "&p2turn=" + p2turn + "&p1total=" + p1total + "&p2total=" + p2total;

i add some extra 'player1name' in your url as well as stripping extra '?'

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.