php - Show chat window when new message received -
i trying build chat system. problem when 1 user types message chat window won't display instantly message receiver. after receiver refreshes window works fine. here current code:
profile.php
setinterval(function(){ var username="<?php echo $username; ?>"; $.ajax({ url:"s/shower.php", type:"post", data:"shower=" + username, success:function(data){ if (data == 1){ $(".pchat").show(); } } }); },500); $(".chat_wind").click(function(){ var id=$(this).attr("id"); var newid=id.split("chat_wind"); var datid=newid[1]; $(".pchat").show(); }); $(".chatform").submit(function(){ var parent=$(this).attr("id"); var split=parent.split("chatform"); var newid=split[1]; var val=$("#chati").val(); if (val.length == 0){ return false; } $.ajax({ url:"s/log.php", type:"post", data:"newid=" + newid + "&txt=" + val, success:function(data){ setinterval(function(){ $.ajax({ url:"s/chat.php", type:"post", data:"username=" + newid, success:function(w){ var objdiv = document.getelementbyid("cbody"); objdiv.scrolltop = objdiv.scrollheight; $("#cbody").html(w); } }); },500); } }); $("#chati").val(""); return false; });
chat.php
<?php include "db.php"; include "timeago.php"; if (isset($_post['username'])){ $username=$_post['username']; $dat=''; $sql=mysql_query("select * chat uchated='$username' order time asc"); while($row=mysql_fetch_array($sql)){ $time=$row['time']; $txt=$row['text']; $dat.=" <div id='msg'> $txt <div id='ctime'></div> </div>"; } echo $dat; } ?>
shower.php
<?php include "db.php"; if (isset($_post['shower'])){ $shower=$_post['shower']; $sqlara=mysql_query("select * chat uchated='$shower'"); $numara=mysql_num_rows($sqlara); if ($numara == 0){ echo "0"; }else{ echo "1"; } } ?>
you need use websockets implement chat system. dont know php. can try link http://www.sanwebe.com/2013/05/chat-using-websocket-php-socket understand websocket programming
Comments
Post a Comment