Commit 848e7fdf authored by Vadym Gidulian's avatar Vadym Gidulian

Fixed some errors and added storing of user name

parent 6a77bb7b
angular.module('chatroom', []) angular
.module('chatroom', [])
.controller('ChatRoomController', ['$scope', '$http', '$interval', function ($scope, $http, $interval) { .controller('ChatRoomController', ['$scope', '$http', '$interval', function ($scope, $http, $interval) {
$scope.newMessage = {};
$scope.name = '';
$scope.newMessage = '';
$scope.messages = []; $scope.messages = [];
$scope.maxMessageId = 0; $scope.maxMessageId = 0;
...@@ -28,9 +31,12 @@ angular.module('chatroom', []) ...@@ -28,9 +31,12 @@ angular.module('chatroom', [])
}; };
$scope.sendMessage = function () { $scope.sendMessage = function () {
$http.post('messages.php', $scope.newMessage) $http.post('messages.php', {
name: $scope.name,
message: $scope.newMessage
})
.success(function () { .success(function () {
$scope.newMessage = {}; $scope.newMessage = '';
}); });
}; };
...@@ -41,4 +47,5 @@ angular.module('chatroom', []) ...@@ -41,4 +47,5 @@ angular.module('chatroom', [])
$interval(function () { $interval(function () {
$scope.checkNewMessages(); $scope.checkNewMessages();
}, 1000); }, 1000);
}]); }]);
...@@ -4,11 +4,12 @@ ...@@ -4,11 +4,12 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Chatroom</title> <title>Chatroom</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="chatroom.js"></script> <script src="chatroom.js"></script>
<link rel="stylesheet/less" href="style.less"> <link rel="stylesheet/less" href="style.less">
<script src="//cdnjs.cloudflare.com/ajax/libs/less.js/2.5.3/less.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/less.js/2.7.1/less.min.js"></script>
</head> </head>
<body ng-app="chatroom" ng-controller="ChatRoomController as chatroom"> <body ng-app="chatroom" ng-controller="ChatRoomController as chatroom">
...@@ -18,23 +19,22 @@ ...@@ -18,23 +19,22 @@
<span class="time" ng-bind="message.date"></span> <span class="time" ng-bind="message.date"></span>
<span class="name" ng-bind="message.name"></span> <span class="name" ng-bind="message.name"></span>
</div> </div>
<p ng-bind="message.message"></p> <p ng-bind="message.message"></p>
</div> </div>
</div> </div>
<div id="controls"> <div id="controls">
<form action="javascript: void(0)" ng-submit="sendMessage()"> <form ng-submit="sendMessage()">
<div> <div>
<input type="text" ng-model="newMessage.name" placeholder="Type your name here"> <input type="text" ng-model="name" placeholder="Type your name here">
</div> </div>
<div> <div>
<input type="text" ng-model="newMessage.message" placeholder="Type your message here"> <input type="text" ng-model="newMessage" placeholder="Type your message here">
<button>Send</button> <button type="submit">Send</button>
</div> </div>
</form> </form>
</div> </div>
<script src="http://code.jquery.com/jquery-2.2.1.min.js"></script>
</body> </body>
</html> </html>
...@@ -2,24 +2,27 @@ ...@@ -2,24 +2,27 @@
$con = mysqli_connect('localhost', 'chatroom', 'chatroom', 'chatroom'); $con = mysqli_connect('localhost', 'chatroom', 'chatroom', 'chatroom');
if (mysqli_connect_errno()) { if (mysqli_connect_errno()) die;
die;
}
switch ($_SERVER['REQUEST_METHOD']) { switch ($_SERVER['REQUEST_METHOD']) {
case 'GET': case 'GET':
// messages.php?id
if (isset($_GET['id'])) { if (isset($_GET['id'])) {
$result = mysqli_query($con, "SELECT MAX(id) AS max_id FROM Messages"); $result = mysqli_query($con, 'SELECT MAX(id) AS max_id FROM Messages');
echo mysqli_fetch_array($result)['max_id']; echo mysqli_fetch_array($result)['max_id'];
die; die;
} }
// messages.php?last=50
$limit = isset($_GET['last']) ? $_GET['last'] : 50; $limit = isset($_GET['last']) ? $_GET['last'] : 50;
$result = mysqli_query($con, "SELECT id, date, username, message FROM Messages LIMIT $limit"); $st = mysqli_prepare($con, 'SELECT id, date, username, message FROM Messages LIMIT ?');
$st->bind_param('i', $limit);
$st->execute();
$result = $st->get_result();
$messages = []; $messages = [];
while($row = mysqli_fetch_array($result)) { while ($row = mysqli_fetch_assoc($result)) {
$message['id'] = $row['id']; $message['id'] = $row['id'];
$message['date'] = $row['date']; $message['date'] = $row['date'];
$message['name'] = $row['username']; $message['name'] = $row['username'];
...@@ -32,13 +35,13 @@ switch ($_SERVER['REQUEST_METHOD']) { ...@@ -32,13 +35,13 @@ switch ($_SERVER['REQUEST_METHOD']) {
break; break;
case 'POST': case 'POST':
$message = json_decode(file_get_contents("php://input")); $message = json_decode(file_get_contents('php://input'));
$name = $message->name; $name = $message->name;
$mess = $message->message; $mess = $message->message;
$st = mysqli_prepare($con, "INSERT INTO Messages (date, username, message) VALUES (NOW(), ?, ?)"); $st = mysqli_prepare($con, 'INSERT INTO Messages (date, username, message) VALUES (NOW(), ?, ?)');
$st->bind_param("ss", $name, $mess); $st->bind_param('ss', $name, $mess);
$result = $st->execute(); $result = $st->execute();
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment