狐狸.fox 2017-07-08 09:10 采纳率: 0%
浏览 8

PHP / AJAX没有得到回报

I am using AJAX to receive data from my database on to my main PHP page. I have a piece of code that worked, but on with PHP. When I have just tried to put it in to AJAX (receiving format), the code that I return is not being shown.

I know my AJAX method works as I'm using it to get some other database values. It's just the Get online users individually won't work.

When I load the page, the code shows what's inside my div id - Loading Info... and then goes blank, so I know it's trying to update it but it's not getting it correctly.

Picture showing that nothing is displayed

My PHP request code is :

//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;

My AJAX recieve code is :

<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>

I then try storing the returned data in-side my div id :

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

1条回答 默认 最新

  • weixin_33725722 2017-07-08 09:32
    关注

    Return the json data like this

    1st : your overwriting the variable . you need to concatenate all user like this

      $client_is_online=""; //declare empty string before while loop start 
     //while loop start here 
    
     $client_is_online .= 'html here';
    
     // while end here 
    

    2nd : Return the json data like this

    $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);
    

    3rd : Don't forgot to add dataType in ajax

    dataType: "json",
    

    4rd : success function should be changed like this

    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']);
            },
    
    评论

报告相同问题?