Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. <?PHP
  2. $dbhost = 'localhost';
  3. $dbuser = 'root';
  4. $dbpass = '';
  5.  
  6. $conn = mysql_connect($dbhost, $dbuser, $dbpass)
  7. or die ('Error connecting to mysql');
  8.  
  9.  
  10.  
  11. mysql_select_db('db');
  12.  
  13. if (isset($_POST['message']))
  14. {
  15. $message = $_POST['message'];
  16. $username = $_SESSION['username'];
  17. }
  18.  
  19. if($message != "")
  20. {
  21. $sql = "INSERT INTO `chat` VALUES('', '$message')";
  22. mysql_query($sql);
  23. }
  24.  
  25. $sql = "SELECT `Text` FROM `chat` ORDER BY `Id` DESC";
  26. $result = mysql_query($sql);
  27.  
  28. while($row = mysql_fetch_array($result))
  29. echo $row['Text']."n";
  30.  
  31.  
  32. ?>
  33.  
  34. <?PHP
  35. session_start();
  36. /*This script makes sure that if there isn't a value for the username in the session(no session), then it will redirect
  37. the person back to the login screen*/
  38. If (($_SESSION['username']) == null) {
  39. header("Location: index.php");
  40. exit;
  41. /*This else statement carries on the code if there is a username*/
  42. }else{
  43. $username = ($_SESSION['username']);
  44. }
  45. ?>
  46. <html>
  47. <head>
  48. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  49. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  50. <link rel="stylesheet" href="/../css/styleindex.css" type="text/css" />
  51. <script type="text/javascript" src="jquery.js"></script>
  52. <script type="text/javascript" src="chatscript.js"></script>
  53. </head>
  54.  
  55. <body>
  56. <?PHP
  57. // Creates a connection to the database
  58. $conn = new mysqli('localhost', 'root', '', 'db');
  59. // Check connection and display error message if incorrect
  60. if ($conn->connect_error) {
  61. die("<div class='alert alert-danger'><strong>Oops!</strong> Connection failed with error: " . $conn->connect_error . "</div>");
  62. }
  63. //This selects all the data from the ticket table and collects a result
  64. $sql = "SELECT * FROM user WHERE username = $username";
  65. $result = $conn->query($sql);
  66. while($row = mysqli_fetch_array($result))
  67. {
  68. echo $row['firstname'] . " " . $row['lastname'];
  69. }
  70. mysqli_close($conn);
  71. ?>
  72. <div class="container">
  73. <textarea class="form-control" id="screen" cols="8" rows="20"> </textarea>
  74. <br>
  75. <input class="form-control" id="message" size="40">
  76. <br>
  77. <button class="btn btn-primary" id="button"> Send </button>
  78. </div>
  79. </body>
  80.  
  81. </html>
  82.  
  83. function update() {
  84. $.post("chatserver.php", {}, function(data) {
  85. $("#screen").val(data);
  86. });
  87.  
  88. setTimeout('update()', 1000);
  89. }
  90.  
  91. $(document).ready(
  92.  
  93. function() {
  94. update();
  95.  
  96. $("#button").click(
  97. function() {
  98. $.post("chatserver.php", {
  99. message: $("#message").val()
  100. },
  101. function(data) {
  102. $("#screen").val(data);
  103. $("#message").val("");
  104. }
  105. );
  106. }
  107. );
  108. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement