Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. <script>
  2. $(document).on("pageinit",function(event){
  3. setInterval(update, 10000);
  4. setInterval(getMessage, 20000);
  5.  
  6. $( "#submit" ).bind( "click", function(event, ui) {
  7. //get message
  8. var msg_display = $("#msg").val();
  9. //get chat_id, if not set, display an alert of not able to send, please select chat member
  10. var chat_id = $("#chat_id").val();
  11. //get user_id
  12. var user = $("#user").val();
  13. if(chat_id != ""){
  14. //send message
  15. sendMessage(msg_display, chat_id, user);
  16. }else{
  17. alert("Please select a user to chat with");
  18. $("#msg").val("");
  19. }
  20. });
  21.  
  22. $( "#m_on li" ).bind( "click", function() {
  23. var selected_member = $(this).html();
  24. window.location = 'chat.php?chat_mate=' + selected_member;
  25. //reload this page with the values of member to chat with
  26. });
  27.  
  28.  
  29.  
  30. function update() {
  31. $.ajax({
  32. type: 'POST',
  33. url: "update.php",
  34. success: function(result){
  35. //all good.
  36. }
  37. })
  38. }
  39. function sendMessage(msg, msg_id, from){
  40. $.ajax({
  41. type: 'POST',
  42. url: "send_message.php",
  43. data: {msg:msg, msg_id:msg_id, from:from},
  44. success: function(result){
  45. if(result == "good"){
  46. getMessage(msg_id);
  47. }else{
  48. alert("Not able to send message " + result);
  49. }
  50. }
  51. })
  52. }
  53.  
  54. function getMessage(msg_id){
  55. $.ajax({
  56. type: 'POST',
  57. url: "get_message.php",
  58. data: {msg_id:msg_id},
  59. dataType: "json",
  60. success: function(result){
  61. //all good.
  62. ///append to the chats.
  63. for(var i=0, i < result.length; i++){
  64. $("#chats").append("<p>" + result[i]['message'] + "</p>" +
  65. "<p align=right>Sent by " + result[i]['sender'] + " at " +
  66. result[i]['time'] + "</p>");
  67. }
  68. $("#msg").val("");
  69. $('#chats').animate({scrollTop:$('#chats').prop("scrollHeight")}, 500);
  70. }
  71. })
  72. }
  73. });
  74. </script>
  75.  
  76. <div id="chats"></div>
  77. <textarea cols="40" rows="8" name="msg" id="msg" placeholder="Message here..."></textarea>
  78. <input type="button" data-inline="true" id="submit" value="Submit">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement