PHP / AJAX没有得到回报

时间:2017-07-08 09:10:15

标签: php ajax

我正在使用AJAX从我的数据库接收数据到我的主PHP页面。 我有一段有效的代码,但是使用PHP。 当我刚尝试将其放入AJAX(接收格式)时,我返回的代码没有显示。

我知道我的AJAX方法可以正常工作,因为我使用它来获取其他一些数据库值。 它只是让在线用户单独赢得工作。

当我加载页面时,代码显示我的div id中的内容 - 加载信息...然后变为空白,所以我知道它正在尝试更新它但是它已经没弄错。

Picture showing that nothing is displayed

我的PHP请求代码是:

//Get online users individually and echo if they're online or not in a div class
$user_grab = mysqli_query($con, "SELECT * FROM users");
while($users_ = mysqli_fetch_array($user_grab)) {

$last_online = strtotime($users_['lastonline']);

if(time() - $last_online < 30) {
    $client_is_online = '
         <div class="chat-list-item -available" style="background: rgba(255,255,255,0.1); padding: 5px;">
            <img class="chat-list-avatar" src="'.$users_['profile_picture'].'" style="width: 40px; height: 40px; padding: 7px; border-radius: 20px;" /><i class="fa fa-circle chat-list-status"> </i>
            <div class="chat-list-user">'.$users_['username'].' (<font size="2">'.get_users_level_all($users_['userLevel']).'</font>)</div>
            <div class="chat-list-excerpt">Online</div>
         </div>
    ';
} else {
    $client_is_online = '
         <div class="chat-list-item -offline" style="background: rgba(255,255,255,0.1); padding: 5px;">
            <img class="chat-list-avatar" src="'.$users_['profile_picture'].'" style="width: 40px; height: 40px; padding: 7px; border-radius: 20px;" /><i class="fa fa-circle chat-list-status"> </i>
            <div class="chat-list-user">'.$users_['username'].' (<font size="2">'.get_users_level_all($users_['userLevel']).'</font>)</div>
            <div class="chat-list-excerpt">Offline</div>
         </div>
    ';
}  
}

//I then echo it back to my home PHP page so it can read the values
//Ignore my other code definitions below as I know they work
//$client_is_online is the only one which doesn't
echo $totalUsers.",".$totalOnline.",".$freemode.",".$bypasses.",".$client_is_online;

我的AJAX收到的代码是:

<script>
    function fetchOnline() {
        $.ajax({
            url: "includes/get_dash_settings.php",
            context: document.body,
            success: function(value){
                var data = value.split(",");
                $('#totalUsers').html(data[0]);
                $('#totalOnline').html(data[1]);
                $('#freeModeStatus').html(data[2]);
                $('#bypassesStatus').html(data[3]);
                $('#isOnline').html(data[4]);
            },
            complete:function(){
               setTimeout(fetchOnline,5000);
            }
        })
    }

    $(document).ready(function() { setInterval(fetchOnline,5000); });
</script>

然后我尝试将返回的数据存储在我的div id:

   <div class="sidebar-tab-content" id="staff">
      <div class="chat-list sidebar-content-section" id="isOnline">
      Loading Info...
      </div>
   </div>

1 个答案:

答案 0 :(得分:1)

像这样返回json数据

1st:你覆盖变量。您需要concatenate所有user这样

  $client_is_online=""; //declare empty string before while loop start 
 //while loop start here 

 $client_is_online .= 'html here';

 // while end here 

第二名:像这样返回json数据

$response = array ('totalUsers'=> $totalUsers, 'totalOnline'=> $totalOnline,'freemode'=>$freemode,'bypasses'=>$bypasses,'client_is_online'=>$client_is_online);
header('Content-Type: application/json');
echo json_encode($response);

第3名:不要忘记在dataType

中添加ajax
dataType: "json",

4rd:成功功能应该像这样改变

ajax

success: function(value){
            var data = JSON.parse(value);
            $('#totalUsers').html(data['totalUsers']);
            $('#totalOnline').html(data['totalOnline']);
            $('#freeModeStatus').html(data['freemode']);
            $('#bypassesStatus').html(data['bypasses']);
            $('#isOnline').html(data['client_is_online']);
        },