douya1061 2016-09-21 06:53
浏览 36
已采纳

ajax php总是返回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?

  • 写回答

1条回答 默认 最新

  • doujianchao7446 2016-09-21 07:28
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)