ajax for displaying database information

天涯浪子 提交于 2019-12-13 00:41:28

问题


I have a script for chatting that Used Ajax,

I would like to do a monitoring script in order to allow the administrator to see all exchanges between users.

The thing is that I did not find any script that explain how to receive data from databse with no reload of the page.

If anybody could help me to find that script or give me a tutorial to build it I'll be glad.

Receive All my utmost Respect.

Kind regards.

SP.


回答1:


You are using ajax.. the whole concept of ajax is not having to reload the page...

The best tools for the job are:

  • websockets. Less overhead. 2 way communication. downside: harder to implement and not cross browser
  • server-sent events. Less overhead. 1 way communication, no repeated requests. downside: not cross browser
  • just plain ajax polling... less efficient, more overhead, many requests but it will work everywhere.

Socket.io (written for nodeJS) is a probably the best for the job. It leverages the right transport (websockets, longpolling, server-sent events, flashsockets, ajax polling) for you and will give you the best performance available.

Here are a couple php js implementation examples:

using Server-sent DOM events (not cross browser): updates +/- every 3s

//javascript:
var source = new EventSource('updates.php');
source.onmessage = function (event) {
  console.log(event.data);
};

//php server side:
<?php
header("Content-Type: text/event-stream\n\n");
//..perform queries and put it in $data..
echo "data: " . json_encode($data) . "\n";
?>

using jQuery ajax request: will send a request every 1000ms

//javascript: 
var interval_id = setInterval(function(){
    $.ajax({
      type: "POST",
      url: "updates.php",
      success: function(data){
        console.log("Data: ", data);
      }
    });
}, 1000); //last param is the interval time in ms

//php server side:
<?php
//..perform queries and put it in $data..
echo json_encode($data) . "\n";
?>

using phpWebsockets: lib + codesample



来源:https://stackoverflow.com/questions/12315857/ajax-for-displaying-database-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!