GregHBushnell GregHBushnell - 2 months ago 7
Ajax Question

ajax php always returning null

Hi im trying to use multple ajax functions which im not sure is possible.

my first one is called when your typing your username

onkeyup="UsernameTaken(this.value);"


the other one is called in the body with

onload="BattlePlayers();"


the functions are like this

var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
function UsernameTaken(name){
if (name == "") {
document.getElementById("UsernameTaken").innerHTML = "";
return;
}
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("UsernameTaken").innerHTML = this.responseText;
}
};
xhttp.open("GET", "CheckUsername.php?q="+name, true);
xhttp.send();
}
function BattlePlayers(){
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("BattleTable").innerHTML = this.responseText;
}
};
xhttp.open("GET", "GetPlayers.php", true);
xhttp.send();
}


then the php for the battleplayer which seems to always return blank is this

<?php
$link = mysqli_connect("","","","");
if (isset($_SESSION['username'])) {
$x = 0;
$sql = "SELECT * FROM userstats ORDER BY RAND() LIMIT 5; ";
$result = mysqli_query($link,$sql);
$toecho ="";
while($row = mysqli_fetch_assoc($result)){
if($row['username'] !== $_SESSION['username']){//add so it dosent put duplicates
$toecho .="<tr>";
$toecho .="<th>".$row['username']." </th>";
$toecho .="<th>Level: ".$row['Level']." </th>";
$toecho .="<th>Player Stats:".$row['Attack']."/".$row['Defence']." </th>";
$toecho .="<th>Win Chance: ";
$toecho .= CalculateWinChance($link,$row['Defence']);
$toecho .="<input type='hidden' name='hidden1' value='".$row['Defence']."' />";
$toecho .="<input type='hidden' name='hidden2' value='".$row['username']."' />";
$toecho .="<th><input type ='submit' name = 'Attack_Btn' value ='Attack'></th>";
$toecho .="</tr>";
}
}
echo $toecho;
}
?>


it does not seem to get to the battleplayers at all i have tried echoing an alert from getplayers with nothing coming up. i have confirmed that the javascript is being called by making that alert at different stages. its just when it reaches the getplayers that is just seems to stop. what am i doing wrong here?

Answer

If you think logically about what your ajax request is doing then you'll realise that if the page being called via ajax needs access to session variables then, because it is effectively a distinct request separate from the page that initiated the request, you will need to include session_start() on that script.

If you had a standard php page like:

<?php
    session_start();

    include 'functions.php';
    include 'classes.php';
    include 'GetPlayers.php';
?>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <!-- stuff -->
    </body>
</html>

In the above example page your script GetPlayers.php WOULD have access to the session.

<?php
    session_start();

    include 'functions.php';
    include 'classes.php';
?>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <script>
            xhr=new XMLHttpRequest();
            xhr.onreadystatechange=function(){
                if( xhr.status==200 && xhr.readyState==4 ){
                    alert( xhr.response );
                }
            };
            xhr.open( 'GET','GetPlayers.php',true );
            xhr.send();
        </script>
    </body>
</html>

Whereas here, without session_start() at the top of GetPlayers.php the two pages do not share the same session and hence when the script checks for if (isset($_SESSION['username'])) {.....} it will fail.

Comments