From 46450e9bbac7c4eecbf70a49bbf5f31e1127441f Mon Sep 17 00:00:00 2001 From: Cole Date: Sun, 3 Jan 2016 15:30:48 -0500 Subject: [PATCH] comments --- src/chat.php | 22 ++++++++++++---------- src/joinChat.php | 2 +- src/updateData.php | 6 ++++-- src/updateMessage.php | 6 +++++- src/wipe.php | 1 + 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/chat.php b/src/chat.php index 2bf5f8f..c3ed109 100644 --- a/src/chat.php +++ b/src/chat.php @@ -1,6 +1,7 @@ $(document).ready(); + //wipes data on the front end and back end function wipeData(){ $.ajax({ url : "wipe.php", @@ -57,6 +59,7 @@ function(data){ }); } + //counts characters and updates div function charCount() { var text = document.getElementById("message").value; var length = text.length; @@ -67,6 +70,7 @@ function charCount() { } window.setInterval(charCount, 250); + //checks with server script to get updated messages and renders them appropriately function refreshData() { $.ajax({ url: "updateData.php", @@ -108,6 +112,7 @@ function(data){ $(divTime).html(time); $(divNum).html(num); + //handles alignment based on the sender if(real_name == name){ $(divMessage).css({"font-weight": "bold", "text-align" : "right", "padding-right" : "25px"}); $(divInfo).css({"font-weight": "bold", "text-align" : "left"}); @@ -124,10 +129,11 @@ function(data){ } window.setInterval(refreshData, 250); - + //sends message to database and renders updated messages function sendMessage(){ var text = document.getElementById("message").value; + //not being used currently var alert = ""; if(text == ""){ alert = "Cannot send a blank message"; @@ -397,15 +403,12 @@ function(data){ - - - - - -

Word Count: 0

+ + +

Word Count: 0

- diff --git a/src/joinChat.php b/src/joinChat.php index fadb66f..bd22b05 100644 --- a/src/joinChat.php +++ b/src/joinChat.php @@ -10,7 +10,7 @@ $_SESSION["chat"] = $chat; $_SESSION["name"] = $name; - + if(!mysql_num_rows(mysqli_query("SHOW TABLES LIKE '".`$chat`."'"))==1){ //table doesn't exits diff --git a/src/updateData.php b/src/updateData.php index 470d2cd..b36040b 100644 --- a/src/updateData.php +++ b/src/updateData.php @@ -6,9 +6,11 @@ require_once('dbconfig.php'); $con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE); + //gathers session variables $name = $_SESSION["name"]; $chat = $_SESSION["chat"]; + //prepares message to be sent to the webpage for rendering $data = []; $iter = 1; @@ -17,11 +19,10 @@ $data[$iter] = $row; $iter++; } - mysqli_close($con); + //adds current user and emptyness information $data["current_user"] = $name; - if(mysqli_num_rows($result) == 0){ $data["empty"] = "true"; } @@ -29,5 +30,6 @@ $data["empty"] = "false"; } + //returns data echo json_encode($data); ?> \ No newline at end of file diff --git a/src/updateMessage.php b/src/updateMessage.php index 0f5dfc7..cf9c7a4 100644 --- a/src/updateMessage.php +++ b/src/updateMessage.php @@ -6,24 +6,29 @@ require_once('dbconfig.php'); $con = mysqli_connect($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_DATABASE); + //gathers information from session and post variables $name = $_SESSION["name"]; $chat = $_SESSION["chat"]; $message = mysqli_escape_string($con, $_POST["message"]); $messageNum = mysqli_num_rows(mysqli_query($con, "select * from `$chat`")); + //creates timestamp $time = date("h:i:sa"); $mydate=getdate(date("U")); $date = $mydate[mon] . "/" . $mydate[mday] . "/" . $mydate[year]; $timestamp = $date . " @ " . $time; + //updates all message numbers by 1 and inserts new message mysqli_query($con,"update `$chat` set m_num = m_num + 1"); mysqli_query($con, "insert into `$chat` (name, message, time, m_num) values ('$name','$message','$timestamp','1')"); + //deletes any overflow message if($messageNum == 20){ mysqli_query($con, "delete from `$chat` where `m_num` = '21'"); } + //prepares data to be sent back for rendering $data = []; $iter = 1; @@ -32,7 +37,6 @@ $data[$iter] = $row; $iter++; } - mysqli_close($con); $data["current_user"] = $name; diff --git a/src/wipe.php b/src/wipe.php index ddd4d06..b7656f7 100644 --- a/src/wipe.php +++ b/src/wipe.php @@ -7,6 +7,7 @@ $chat = $_SESSION["chat"]; + //empties the table for the specified chat mysqli_query($con, "delete from `$chat`"); mysqli_close($con); ?> \ No newline at end of file