在MySQL中插入新行时,如何编写Ajax代码以获取新行? [英] How to write an Ajax code to fetch new row when it is inserted in MySQL?

查看:51
本文介绍了在MySQL中插入新行时,如何编写Ajax代码以获取新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的聊天服务.我将消息,登录名和时间等存储在Mysql数据库中.聊天消息在Ajax& A的帮助下显示在下面.PHP

I have a simple chat service. I store messages, login and time etc. in Mysql database. The chat messages are displayed below with the help of Ajax & PHP

<div id="messages"> </div><!--- chats are displayed here -->

我有以下Ajax代码,每2秒从Mysql中获取数据.当然,每个人都建议不要这样做.这可能会对服务器性能产生负面影响.而且这是不必要的.

I have following Ajax code which fetches datas from Mysql in every 2 seconds. Ofcourse, everyone recommends not to do this way. It might effect server performances negatively. And its unnecessary.

$(document).ready( function() {

     var destination_hashed = $("#destination_hashed").val();

     var interval = setInterval( function() {

            $.ajax ({
                     type: "POST",
                     url: "chat.php",
                     data: { destination_hashed1: destination_hashed },

                     success: function(data) {
                            $("#messages").html(data);
                     }
             });
     }, 2000);
});

简而言之,我有两个聊天客户端A和B.当A向B发送消息时,在Mysql中插入了新行.

In nutshell, I have two chat clients A & B. When A sends message to B, there are new rows inserted in Mysql.

那么,我该如何写Ajax& ;?仅在有新行时才获取PHP代码.不管是否插入新行,而不是每2秒从Mysql中获取数据

So, how can I write Ajax & PHP code to fetch only when there are new rows. Rather than fetching data from Mysql every 2 seconds whether new rows inserted or not

推荐答案

最近,我正在研究这种聊天模块,我可以在您的代码中说些更正

Recently I have worked on a chat module of this kind and I can say some correction in you code

首先不要以您使用的方式使用 setInterval

First of all don't use setInterval in the way you are using ,

为什么

因为在您的特定代码中,请求每2秒发送到服务器一次,所以如果网络速度较慢,您的代码将倾向于向服务器发送大量请求,并且这些请求之间的协调将很困难.

because in your particular code the request is send to the server every 2 second , so if the network is slow your code will have the tendency to send to much request to the server and coordination between those request would be difficult.

所以你要做的是

function getNewMessage(){

            $.ajax ({
                     type: "POST",
                     url: "chat.php",
                     data: { destination_hashed1: destination_hashed },

                     success: function(data) {
                            $("#messages").html(data);
                     },
                     complete : function(){  // this will be called even if our request fail, success etc
                         setTimeout(function(){   // send request after 2 second from the complition of the previous request
                             getNewMessage();       
                         },2000);
                     }
             });

}

解析ajax调用的聊天结果有两种方法

There are two approach in parsing the chat result from the ajax call

a)从ajax调用中获取html(如果您将来要扩展模块,这将很慢且效率低下)

a) get the html from the ajax call (this would be slow and inefficient in case you want to extend your module in the future)

您可以简单地从succes参数的ajax调用中获取html内容,可以将其附加到表中

You can simpley get the html content from the ajax call on succes parameter you can append that to the table

 For example : -

   if the response from the server on new message is 

     <div class="chat"> 
       <lable>John</lable>
         Hi there !!!
    </div>

然后解析内容将是这样

             success: function(data) {
                    // $("#messages").html(data);   //this will change the content in every request
                    $("#messages").append(data);   // this will append the new conent
             },

b)另一个方法是获取json格式的数据(更好的方法)

b) Well the other approch would be to get data in json format(a lot better approch )

例如:-

   if the response from the server on new message is 

     { 
       username : 'john',
       message  : 'Hi there !!!'
     }

然后解析内容将是这样

             success: function(data) {
                    // $("#messages").html(data);  this will change the content in every request
                    $("#messages").append('<div class="chat">'+
                                            '<lable>'+data.username+'</lable>'+
                                              data.message +
                                          '</div>');    this will append the new conent
             },  

这篇关于在MySQL中插入新行时,如何编写Ajax代码以获取新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆