简体   繁体   English

ajax php总是返回null

[英]ajax php always returning null

Hi im trying to use multple ajax functions which im not sure is possible. 您好,我试图使用不确定的ajax函数。

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似乎总是返回空白

   <?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. 我尝试从getplayer发出警报,但没有任何反应,这似乎根本没有吸引到战斗玩家。 i have confirmed that the javascript is being called by making that alert at different stages. 我已经确认通过在不同阶段发出警报可以调用javascript。 its just when it reaches the getplayers that is just seems to stop. 它只是在到达getplayers时才停止。 what am i doing wrong here? 我在这里做错了什么?

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. 如果您从逻辑上考虑ajax请求在做什么,那么您会意识到,如果通过ajax调用的页面需要访问会话变量,那么,由于它实际上是与发起请求的页面不同的请求,因此您将需要在该脚本上包含session_start()

If you had a standard php page like: 如果您有一个标准的php页面,例如:

<?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. 在上面的示例页面中,您的脚本GetPlayers.php有权访问该会话。

<?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. 而在这里,在GetPlayers.php顶部没有session_start()情况下,两个页面不会共享同一会话,因此脚本在检查if (isset($_SESSION['username'])) {.....}它会失败。

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

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