Posted By

olesla on 06/23/15


Tagged

php jquery


Versions (?)

php + jquery chat


 / Published in: jQuery
 

durr

  1. <?php
  2. if($_SERVER['REQUEST_METHOD'] == 'POST')
  3. {
  4. header("Content-Type: text/html; charset=utf-8");
  5. session_start();
  6. require_once("functions.php");
  7. if ($_POST['action']=='update')
  8. {
  9. $result = $slaattene_db->query("SELECT * FROM chat ORDER BY time DESC LIMIT 10");
  10. $return = '<table>';
  11. while ($row = $result->fetch_assoc())
  12. {
  13. $message[]=$row;
  14. }
  15. $message=array_reverse($message);
  16. foreach($message as $msg)
  17. {
  18. if(substr($msg['message'], 0, 4 ) === "http" || substr( $msg['message'], 0, 3 ) === "www") // <a href="www.google.com">google.com</a>
  19. {
  20. $return .= '<tr><td><font color="#32cd32">' . substr($msg['time'], -8) . '</font></td><td><strong>' . $msg['handle'] . ':</strong></td><td> <a href="'.$msg['message'].'">'.$msg['message'].'</a></td></tr>';
  21. }
  22. else
  23. {
  24. $return .= '<tr><td><font color="#32cd32">' . substr($msg['time'], -8) . '</font></td><td><strong>' . $msg['handle'] . ':</strong></td><td> ' . $msg['message'] . '</td></tr>';
  25. }
  26. }
  27. $return .= '</table>';
  28. die($return);
  29. }
  30. if ($_POST['action']=='send')
  31. {
  32. $time = date("Y-m-d H:i:s");
  33. $handle=$_POST['handle'];
  34. $message=$_POST['message'];
  35. $slaattene_db->query("INSERT INTO chat (handle, message, time) VALUES ('$handle','$message','$time')");
  36. die();
  37. }
  38. if ($_POST['action']=='updateusers')
  39. {
  40. $handle=$_POST['handle'];
  41. $activeUsers=$slaattene_db->query("SELECT * FROM chat_current_users WHERE lastseen > NOW() - INTERVAL 2 MINUTE");
  42. $time = date("Y-m-d H:i:s");
  43. $slaattene_db->query("UPDATE chat_current_users SET lastseen='$time' WHERE handle='$handle'");
  44. $return='<table><tr><strong>Online:</strong></tr>';
  45. while($row=$activeUsers->fetch_assoc())
  46. {
  47. $return.='<tr><td>'.$row['handle'].'</td></tr>';
  48. }
  49. $return.='</table>';
  50. die($return);
  51. }
  52. if($_POST['action']=='sethandle')
  53. {
  54. $handle=$_POST['handle'];
  55. $checkHandle=$slaattene_db->query("SELECT * FROM chat_current_users WHERE handle='$handle'");
  56. if($checkHandle->num_rows == 0)
  57. {
  58. $time = date("Y-m-d H:i:s");
  59. $slaattene_db->query("INSERT INTO chat_current_users (handle, lastseen) VALUES ('$handle','$time')");
  60. }
  61. else
  62. {
  63. $slaattene_db->query("UPDATE chat_current_users SET lastseen='$time' WHERE handle='$handle'");
  64. }
  65. }
  66. die();
  67. }
  68. echo '<head><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script></head>';
  69.  
  70. if (!isset($_GET['p']))
  71. {
  72. echo '
  73. <table width=1024>
  74. <tr>
  75. <td width=800><div id=chatwindow style="margin: 10px 0 10px 0;"></div></td><td valign="top"><div id=showusers style="margin: 10px 0 10px 0;"></div></td></tr>
  76. <tr>
  77. <td><input type="text" id="message" size=50 placeholder="message">
  78. <input type="button" value="Send" onclick="send_message();" />
  79. </td><td><input type="text" name="handle" id="handle" placeholder="Type a name"><input type="button" value="Set" onclick="set_handle();" /></td>
  80. </td>
  81. <td>
  82. </td>
  83. </tr>
  84. </table>
  85. <script>
  86. function update_chat() {
  87. $.post("index.php", {
  88. action:"update"
  89. },
  90. function(data) {
  91. $("#chatwindow").html(data);
  92. }
  93. );
  94. }
  95. function update_users() {
  96. $.post("index.php", {
  97. action:"updateusers",
  98. handle:$("#handle").val()
  99. },
  100. function(data) {
  101. $("#showusers").html(data);
  102. }
  103. );
  104. }
  105. function set_handle()
  106. {
  107. $.post("index.php", {
  108. action:"sethandle",
  109. handle:$("#handle").val()
  110. },
  111. function(data) {
  112. $("#showusers").html(data);
  113. }
  114. );
  115. }
  116. function send_message() {
  117. if($("#handle").val())
  118. {
  119. $.post("index.php", {
  120. action:"send",
  121. handle:$("#handle").val(),
  122. message:$("#message").val()
  123. },
  124. function(data) {
  125. $("#message").val("");
  126. update_chat();
  127. }
  128. );
  129. }
  130. else
  131. {
  132. alert("You need a handle before chatting!");
  133. }
  134. }
  135. $("#message").keydown(function(e){
  136. if (e.keyCode == 13)
  137. {
  138. send_message();
  139. }
  140. });
  141. setInterval(function(){
  142. update_chat();
  143. update_users();
  144. }, 3500);
  145. </script>';
  146. }
  147. ?>

Report this snippet  

You need to login to post a comment.