2016-09-21 221 views
0

嗨即時嘗試使用多重Ajax功能,即時消息不知道是可能的。ajax php總是返回null

我的第一個被稱爲當你輸入你的用戶名

onkeyup="UsernameTaken(this.value);" 

另一種被稱爲在身體與

onload="BattlePlayers();" 

功能都是這樣

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

然後似乎總是返回空白的戰鬥機的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; 
     } 
?> 

它似乎並沒有得到戰鬥玩家的幫助,我曾嘗試在沒有任何東西的情況下回應來自getplayers的警報。我已經確認,JavaScript是通過在不同階段發出警報來調用的。它只是當它到達玩家似乎停止。我在這裏做錯了什麼?

+1

'session_start()'在哪裏? –

+0

多數民衆贊成在我的主要PHP區域 – GregHBushnell

+1

我的索引頁頂部你需要在'GetPlayers.php'的頂部包含'session_start()'太 – RamRaider

回答

1

如果你從邏輯上思考你的ajax請求在做什麼,那麼你會意識到,如果通過ajax調用的頁面需要訪問會話變量,那麼,因爲它實際上是一個獨立於發起請求的頁面的獨立請求,您需要在該腳本中包含session_start()

如果你有一個標準的PHP頁面,如:

<?php 
    session_start(); 

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

在上面的示例頁面腳本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> 

而在這裏,沒有session_start()GetPlayers.php頂部的兩個頁面不共享同一個會話,因此當if (isset($_SESSION['username'])) {.....}腳本檢查將失敗。