Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
cts5ws committed Jan 3, 2016
1 parent dd311cf commit 46450e9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
22 changes: 12 additions & 10 deletions src/chat.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
session_start();

//checks if session is v alid
if(!isset($_SESSION["valid"])){
$_SESSION["valid"] = "true";
}
Expand Down Expand Up @@ -37,6 +38,7 @@
<script>
$(document).ready();

//wipes data on the front end and back end
function wipeData(){
$.ajax({
url : "wipe.php",
Expand All @@ -57,6 +59,7 @@ function(data){
});
}

//counts characters and updates div
function charCount() {
var text = document.getElementById("message").value;
var length = text.length;
Expand All @@ -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",
Expand Down Expand Up @@ -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"});
Expand All @@ -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";
Expand Down Expand Up @@ -397,15 +403,12 @@ function(data){
</div>
</table>

<td><input type="text" id="message" name="message" size="100" maxlength="110" onkeydown="if (event.keyCode == 13) document.getElementById('button').click()" autocomplete="off">


<button class="btn btn-primary" id="button" name="button" value="Send" onclick="sendMessage();">
<span class="glyphicon glyphicon-send"></span>
</button>

<div id="count" class="row col-center"><h4><span class="label label-default">Word Count: 0</span></h4></div>
<input type="text" id="message" name="message" size="100" maxlength="110" onkeydown="if (event.keyCode == 13) document.getElementById('button').click()" autocomplete="off">

<button class="btn btn-primary" id="button" name="button" value="Send" onclick="sendMessage();">
<span class="glyphicon glyphicon-send"></span>
</button>
<div id="count" class="row col-center"><h4><span class="label label-default">Word Count: 0</span></h4></div>
</div>

<div id="footer" class="mastfoot" style="text-align: center;" >
Expand All @@ -416,6 +419,5 @@ function(data){
</b><em>Copyright &copy; 2015 &middot; All rights reserved.</em>
</div>
</div>

</body>
</html>
2 changes: 1 addition & 1 deletion src/joinChat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/updateData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,17 +19,17 @@
$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";
}
else{
$data["empty"] = "false";
}

//returns data
echo json_encode($data);
?>
6 changes: 5 additions & 1 deletion src/updateMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,7 +37,6 @@
$data[$iter] = $row;
$iter++;
}

mysqli_close($con);

$data["current_user"] = $name;
Expand Down
1 change: 1 addition & 0 deletions src/wipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

$chat = $_SESSION["chat"];

//empties the table for the specified chat
mysqli_query($con, "delete from `$chat`");
mysqli_close($con);
?>

0 comments on commit 46450e9

Please sign in to comment.