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; }) 

  1. use ajax submit message , add database.

    $.post("myscript.php", {userid: 123, message: "my message"});

  2. 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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -