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是通過在不同階段發出警報來調用的。它只是當它到達玩家似乎停止。我在這裏做錯了什麼?
'session_start()'在哪裏? –
多數民衆贊成在我的主要PHP區域 – GregHBushnell
我的索引頁頂部你需要在'GetPlayers.php'的頂部包含'session_start()'太 – RamRaider