HelloDearSir

messages.php

May 20th, 2020
472
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. <?php
  2. session_start();
  3. if($_SESSION['login'] != "That GRRRRREAT") {
  4. header('Location: login.php');
  5. exit();
  6. } else {
  7. $username = $_SESSION['username'];
  8. }
  9. ?>
  10. <!DOCTYPE html>
  11. <html>
  12. <head>
  13. <?php require 'meta.php' ?>
  14. <?php require 'headerlogin.php'?>
  15.  
  16. <link rel="stylesheet" type="text/css" href="css/main.css">
  17. <meta content="width=device-width, initial-scale=1" name="viewport" />
  18. <meta charset="utf-8">
  19. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  20.  
  21.  
  22.  
  23. </head>
  24. <body>
  25.  
  26. <?php require 'newmessage.php' ?>
  27. <?php require 'LeftSide.php' ?>
  28.  
  29. <div id="rightside">
  30.  
  31. <div id="mainmessages">
  32. <?php
  33. require_once 'message.php';
  34. require_once 'connection.php';
  35. /*
  36. selecting from the messages database with the varibles that have been created.
  37. $_GET['user'] is header //('Location:profilepage.php?user='.$Ureciver);
  38. every time user='.$Ureciver it would show each different message when the user has sent a new message to a user.
  39. */
  40. $query= 'SELECT * FROM `chat` WHERE `messager`= "'.$_SESSION['username'].'"
  41. AND `messagee` ="'.$_GET['user'].'"
  42. OR `messager` = "'.$_GET['user'].'"
  43. AND `messagee` = "'.$_SESSION['username'].'" ';
  44. $results = mysqli_query($connect, $query);
  45. if ($results)
  46. { //if the query is successful then do this.
  47. while ($row = mysqli_fetch_assoc($results))
  48. {
  49.  
  50. //$user_sender = $row['messager'];, in the database.
  51.  
  52. // this will be the same through out the whole database..
  53. $user_sender= $row['messager'];
  54. $Ureciver= $row['messagee'];
  55. $message = $row['msg'];
  56. //checking the sender is the user from the seesion.
  57. if($user_sender == $_SESSION['username'])
  58. { ?>
  59. <div class="users">
  60. <p> <?php echo $_SESSION['username']; ?></p>
  61. <p> <?php echo $message ?></p>
  62. </div>
  63. <?php
  64. }
  65. else
  66. {
  67. // showing the message in white, to the reciver.
  68. // this echo will post the $message that that the sender has sent them.
  69. ?>
  70. <div class="recivers">
  71. <p><?php echo $user_sender; ?> </p>
  72. <p><?php echo $message ?></p>
  73. </div>
  74. <?php
  75. }
  76. }
  77. }
  78. ?>
  79. </div>
  80. <div class="container">
  81. <div class="row">
  82.  
  83. <div class="col-md-2">
  84. </div>
  85. <div class=" col-12 col-md-10">
  86. <form method="post" id="message-form" >
  87. <textarea class="textarea" id="message_text" placeholder= "Write your message"></textarea>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </body>
  95. <script src="jquery-3.1.1.min.js"> </script>
  96. <script>
  97. $("document").ready(function(event)
  98. {
  99. //now if the fomr is sumbitted
  100. $("#rightside").on('submit', '#message-form', function(){
  101. //take the data from input
  102. var message_text = $("#message_text").val();
  103. //post it to sned.php
  104. $.post("send.php?user=<?php echo $_GET['user']?>",
  105. { text:message_text,
  106.  
  107. },
  108. function(data,status)
  109. {
  110. alert(data);
  111. $("#message_text").val("");
  112. document.getElementById("mainmessages").HTML += data;
  113. }
  114. );
  115. });
  116. $("#rightside").keypress(function(event)
  117. {
  118. //sumbits when enter is only pressed
  119. if(event.keyCode == 13 && !event.shiftKey)
  120. {
  121.  
  122. // $("#message-form").submit();
  123.  
  124.  
  125. console.log($("#message_text").val(""));
  126. }
  127. });
  128. });
  129. </script>
  130. </html>
Add Comment
Please, Sign In to add comment