Chat with PHP, AJAX and Mysql

Asked

Viewed 1,554 times

-2

Good community. I am working on this chat system, very simple.

I don’t understand why I don’t get messages in real time. I can only receive messages from another user if you close and open the div again, then the message appears.

How can I fix it? As I am not very comfortable with ajax I ask for your help.

I think the problem will be in the last function, message cycle:

function message_cycle()
{   
    $.ajax({
        url:'chat.class.php',
        type:'POST',
        data:'unread=true',

        dataType:'JSON',
        success:function(data){             
            $.each(data , function( index, obj ) {
                var user = index;                   
                var box  = $("#jd-chat").find("div#2").parents(".jd-user");

                $(".jd-online").find(".light").hide();

                $.each(obj, function( key, value ) {
                    if($.inArray(user,open) !== -1 )                                            
                        $(box).find(".jd-body").append("<span style='display:block'  class='other'> " + value + "</span>");                     
                    else            
                        snd.play();
                        $(".jd-online").find("span#" + user + " .light").show();        


                });
            });             
        }
    });
}

setInterval(message_cycle,1000);
});  

Any idea?

I found out where the problem was instead of div id = 2

var box = $("#jd-chat").find("div#2").parents(".jd-user");

called for by span that receives text from user Sender and user receiver

var box = $("#jd-chat").find(".me").parents(".jd-user");

she now receives the messages and soon shows the messages q exist without closing and open the div

  • Hello I’ll give a suggestion a few months ago I was using Mibew he is very good, give a look at this link https://www.youtube.com/watch?v=nWa8svuraC0

  • https://www.youtube.com/watch?v=iyg_fk36p0Y&list=PLBOh8f9FoHHjz91tWQ5AtBoVwOVhP1Di7 has everything you need here and a source code https://github.com/howCodeORG/Messenger

2 answers

1

Try to use and see if it solves

$(document).ready(function() {
$.ajaxSetup({ cache: false });
});

in so-called attempts

$.ajax({ 
url:'chat.class.php',
type:'POST',
cache:false,
...
  • Hello @Arsom Thanks for the reply. I put $.ajaxSetup({ cache: false }); after the first line of my document $(document).ready(function() { and continues with the same problem. I only receive the messages if close the div that receives the messages and call her back

  • try using $.ajax({ url:'chat.class.php', type:'POST',cache:false, in the now called

  • I have cached:false on this line of my document, and it remains the same.

1

  • I could not load messages with the div open. so it shows the div blank

Browser other questions tagged

You are not signed in. Login or sign up in order to post.