javascript - live chat using php/jquery/ajax -


i have question regarding style of .js script updates chatbox on website.

currently, polling .html file keeps chat lines change poll .html file on new entry.

currently working via polling every 1500 , updates chat users:

// jquery document $(document).ready(function(){     //if user submits form     $("#submitmsg").click(function(){         var clientmsg = $("textarea#usermsg").val();         $.post("post.php", {text: clientmsg});                       $("#usermsg").attr("value", "");         clientmsg = "";         $("textarea#usermsg").val('');         return false;     });      $("textarea#usermsg").keydown(function (e) {       if (e.keycode == 13) {         var clientmsg = $("textarea#usermsg").val();         $.post("post.php", {text: clientmsg});                       $("#usermsg").attr("value", "");         clientmsg = "";         $("textarea#usermsg").val('');         return false;       }     }); });  setinterval (function(){ loadlog() }, 1500);  //load file containing chat log function loadlog(){          var oldscrollheight = $("#chatbox").prop("scrollheight") - 20; //scroll height before request     $.ajax({         url: "log.html",         cache: false,         success: function(html){                     $("#chatbox").html(html); //insert chat log #chatbox div                 //auto-scroll                        var newscrollheight = $("#chatbox").prop("scrollheight") - 20; //scroll height after request             if(newscrollheight > oldscrollheight){                 $("#chatbox").animate({ scrolltop: newscrollheight }, 'normal'); //autoscroll bottom of div             }                        },     }); } 

i tried changing call loadlog() function after user presses send/enter noticed load log user pressing send/enter , not other users. , not show line (perhaps executing loadlog() faster posting post.php , writing log file new line?

//load file containing chat log function loadlog(){          var oldscrollheight = $("#chatbox").prop("scrollheight") - 20; //scroll height before request     $.ajax({         url: "log.html",         cache: false,         success: function(html){                     $("#chatbox").html(html); //insert chat log #chatbox div                 //auto-scroll                        var newscrollheight = $("#chatbox").prop("scrollheight") - 20; //scroll height after request             if(newscrollheight > oldscrollheight){                 $("#chatbox").animate({ scrolltop: newscrollheight }, 'normal'); //autoscroll bottom of div             }                        },     }); }  // jquery document $(document).ready(function(){     //if user submits form     $("#submitmsg").click(function(){         var clientmsg = $("textarea#usermsg").val();         $.post("post.php", {text: clientmsg});                       $("#usermsg").attr("value", "");         clientmsg = "";         $("textarea#usermsg").val('');         loadlog();         return false;     });      $("textarea#usermsg").keydown(function (e) {       if (e.keycode == 13) {         var clientmsg = $("textarea#usermsg").val();         $.post("post.php", {text: clientmsg});                       $("#usermsg").attr("value", "");         clientmsg = "";         $("textarea#usermsg").val('');         loadlog();         return false;       }     }); }); 

is there way make second version work update users not user sending data?

html chat:

<div id="chatbox"><?php     if(file_exists("log.html") && filesize("log.html") > 0){         $handle = fopen("log.html", "r");         $contents = fread($handle, filesize("log.html"));         fclose($handle);          echo $contents;     }     ?> </div> <form name="message" action="">     <?php if($loggedin) {          echo '         <textarea name="usermsg" id="usermsg" rows="2"></textarea>         <input name="submitmsg" type="submit"  id="submitmsg" value="send" />';     }      ?> </form> 

post.php

if(isset($steamprofile['steamid'])){     $text = $_post['text'];      $fp = fopen("log.html", 'a');     fwrite($fp, "<div class='msgln'>(".date("g:i a").") <b>".$steamprofile['personaname']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");     fclose($fp); } 

thanks in advance.


Comments