简体   繁体   English

在php,mysql和ajax jquery中开发Chat模块

[英]develop Chat module in php, mysql and ajax jquery

I am trying to implement a small chat application where user can text chat with any one of the online users. 我正在尝试实现一个小型聊天应用程序,用户可以在其中与任何一个在线用户进行文本聊天。 My logic behind this is some thing like the following: 我背后的逻辑是这样的:

 Login first.

 Fetch the users who are online from DB and show them as list of online users.

 Click on the users, then another small window is opening for text chatting.

 Create a form(two hidden fields- one is for sender id and another is for receiver id, one textarea and a button for submitting) for this chatting.

 Through jQuery, fill the value of receiver id.

 By session id, fill the value of sender id.

 After submitting the button, I call a page through ajax jquery which is responsible to insert and show the current data from DB.

My code for the ajaxJquery is like : 我的ajaxJquery代码如下:

$(document).ready(function(){
        $('#send_btn').click(function(){
            var receiver_id = $('#hide_receiver_id').val();
            var sender_id = $('#hide_sender_id').val();
            var messagebox = $('#messagebox').val();

            $.ajax({
                type:"POST",
                url:"chat_history.php?receiver_id="+receiver_id+"&sender_id="+sender_id+"&message="+messagebox,
                success:function(result){
                    $('#history').html(result);
                }   

            });
            $('#messagebox').val('');
        });
    });
</script>

Up to this, its working fine. 到此为止,它的工作正常。 But I need to autoload the <div id="history"></div> portion. 但是我需要自动加载<div id="history"></div>部分。 For that also I am thinking to do by using setInterval() in jQuery. 为此,我也在考虑通过在jQuery中使用setInterval()来实现。 My code is like : 我的代码是这样的:

<script type="text/javascript">
  var auto_refresh = setInterval(
   function (){
    $('#history').load("chat_history.php?receiver_id=''&sender_id=<?php echo $_SESSION['id']?>&message=").fadeIn("fast");
   }, 1000); // refresh every 1000 milliseconds
</script>

But in this scenario, how to pass the value of receiever_id in load() which is necessary to find out the respective data from DB? 但是在这种情况下,如何在load()传递receiever_id的值,这对于从DB中查找相应数据是必需的? Please let me know whether the requirement is cleared to you or not. 请让我知道是否已清除您的要求。

Thanks in advance. 提前致谢。

<script>
    $(function () {

        // function wide variables
        var receiver_id = $('#hide_receiver_id').val();
        var sender_id = $('#hide_sender_id').val();
        var messagebox = $('#messagebox').val();

        // button click
        $('#send_btn').click(function () {
            receiver_id = $('#hide_receiver_id').val();
            sender_id = $('#hide_sender_id').val();
            messagebox = $('#messagebox').val();

            $.ajax({
                type : "POST",
                url : "chat_history.php?receiver_id=" + receiver_id + "&sender_id=" + sender_id + "&message=" + messagebox,
                success : function (result) {
                    $('#history').html(result);
                }
            });
            $('#messagebox').val('');
        });

        var auto_refresh = setInterval(function(){
            $('#history').load("chat_history.php?receiver_id="+receiver_id+"&sender_id=<?php echo $_SESSION['id']?>&message=").fadeIn("fast");
        }, 1000); // refresh every 1000 milliseconds
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM