Chat with PHP, AJAX and MySQL

-2

Good community. I'm working on this very simple chat system.

I do not understand why I do not get the messages in real time. I can only receive messages from another user if I close and back open the div, then the message already appears.

How can I solve it? As I'm 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 ideas?

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

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

I called for the span that receives the user sender and user receiver text

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

It now receives the messages and immediately shows the messages that exist without having to close and open the div

    
asked by anonymous 06.02.2016 / 15:41

2 answers

1

Try to use and see if it works out

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

in so-called try

$.ajax({ 
url:'chat.class.php',
type:'POST',
cache:false,
...
    
06.02.2016 / 17:23
1

Try to update your div temporarily ...

in Javascript:

var tempo = window.setInterval(carrega, 1000);
function carrega()
{
$('#counteudo').load('teste.html');
}

In AJAX you have an example here: link

    
06.02.2016 / 23:39