Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- $dbhost = 'localhost';
- $dbuser = 'root';
- $dbpass = '';
- $conn = mysql_connect($dbhost, $dbuser, $dbpass)
- or die ('Error connecting to mysql');
- mysql_select_db('db');
- if (isset($_POST['message']))
- {
- $message = $_POST['message'];
- $username = $_SESSION['username'];
- }
- if($message != "")
- {
- $sql = "INSERT INTO `chat` VALUES('', '$message')";
- mysql_query($sql);
- }
- $sql = "SELECT `Text` FROM `chat` ORDER BY `Id` DESC";
- $result = mysql_query($sql);
- while($row = mysql_fetch_array($result))
- echo $row['Text']."n";
- ?>
- <?PHP
- session_start();
- /*This script makes sure that if there isn't a value for the username in the session(no session), then it will redirect
- the person back to the login screen*/
- If (($_SESSION['username']) == null) {
- header("Location: index.php");
- exit;
- /*This else statement carries on the code if there is a username*/
- }else{
- $username = ($_SESSION['username']);
- }
- ?>
- <html>
- <head>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
- <link rel="stylesheet" href="/../css/styleindex.css" type="text/css" />
- <script type="text/javascript" src="jquery.js"></script>
- <script type="text/javascript" src="chatscript.js"></script>
- </head>
- <body>
- <?PHP
- // Creates a connection to the database
- $conn = new mysqli('localhost', 'root', '', 'db');
- // Check connection and display error message if incorrect
- if ($conn->connect_error) {
- die("<div class='alert alert-danger'><strong>Oops!</strong> Connection failed with error: " . $conn->connect_error . "</div>");
- }
- //This selects all the data from the ticket table and collects a result
- $sql = "SELECT * FROM user WHERE username = $username";
- $result = $conn->query($sql);
- while($row = mysqli_fetch_array($result))
- {
- echo $row['firstname'] . " " . $row['lastname'];
- }
- mysqli_close($conn);
- ?>
- <div class="container">
- <textarea class="form-control" id="screen" cols="8" rows="20"> </textarea>
- <br>
- <input class="form-control" id="message" size="40">
- <br>
- <button class="btn btn-primary" id="button"> Send </button>
- </div>
- </body>
- </html>
- function update() {
- $.post("chatserver.php", {}, function(data) {
- $("#screen").val(data);
- });
- setTimeout('update()', 1000);
- }
- $(document).ready(
- function() {
- update();
- $("#button").click(
- function() {
- $.post("chatserver.php", {
- message: $("#message").val()
- },
- function(data) {
- $("#screen").val(data);
- $("#message").val("");
- }
- );
- }
- );
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement