Live chat system using jQuery and PHP -
i created chat box jquery , ajax problem chat box not live. if want newest record have refresh page. code quite simple , know best way make live.
this jquery script :
$('#textbox').keypress(function(event) { if(event.which == 13){ $('#sendbutton').click(); event.preventdefault(); } }); $('.chatwindow').load('load.php'); $('#post').submit(function() { var userid = "<?php echo $_session["userid"]; ?>"; var username = "<?php echo $_session["username"]; ?>"; var message = $('#textbox').val(); $.post('post.php', { message: message, username: username, userid: userid }, function(data){ $('.chatwindow').append('<span class="msg" >' + username + ': </span>' + data + '</span><br>'); $('#textbox').val(""); $('.chatwindow').scrolltop($('.chatwindow').prop('scrollheight')); }); return false; })
use ajax submit message , add database.
$.post("myscript.php", {userid: 123, message: "my message"});
every several seconds (i.e. 10 seconds) request newer messages ajax , prepend (or append) discussion div.
window.setinterval(function() { $.post("myscript.php", {action: "refresh", lastid: 1234}, function(response) { $("#discussion").prepend(response.html); lastid = response.lastid; }, "json"); }, 10000);
Comments
Post a Comment