Answer the question
In order to leave comments, you need to log in
How to track new entries in the database and display them on the screen without reloading the page?
Hello. I'm trying to write something like a chat. Wrote adding messages to the database, and their output. But the essence of the task is that new messages themselves would be displayed on the screen without reloading the page. This needs to be done through ajax request (by task) I don’t understand how to write logic so that the request tracks the appearance of new records and displays new messages without reloading. At what records can be added by other chat participants.
This is the message output and form markup:
<?php
$query = $pdo->query('SELECT * FROM `chat`');
while ($row = $query->fetch(PDO::FETCH_OBJ)){
echo "<div class='alert alert-info d-flex justify-content-between'><div>$row->message</div><div><em>$row->login</em></div></div>";
}
?>
<div id="errorBlock" class="error_block alert alert-danger mt-2"></div>
<form id="chat_form" method="post">
<label for="message">Ваше сообщение:</label>
<textarea id="message" type="text" name="message" class="form-control"></textarea>
<button id="mes_send" class="btn btn-success mt-4" type="button">Отправить!</button>
</form>
$('#mes_send').click(function () {
var message = $('#message').val();
$.ajax({
url: 'ajax/chat.php',
type: 'POST',
cache: false,
data: {'message' : message},
dataType: 'html',
success: function(data) {
if(data == 'Готово') {
$('#mes_send').text('Всё готово');
$('#errorBlock').hide();
$('#chat_form').trigger('reset');
setTimeout(function(){
$('#mes_send').text('Отправить!');
}, 900);
}
else {
$('#errorBlock').show();
$('#errorBlock').text(data);
}
}
})
})
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
$error ='';
if(strlen($message) <= 3)
$error ='Введите нормальное сообщение!';
if ($error !='') {
echo $error;
exit();
}
require_once '../mysql_connect.php';
$sql = 'INSERT INTO chat(message, login) VALUES(:message, :login)';
$query = $pdo->prepare($sql);
$query->execute(['message' => $message, 'login' => $_COOKIE['login']]);
echo "Готово";
setInterval(function() {
$.ajax({
url: 'ajax/chat_show.php',
type: 'POST',
cache: false,
data: {},
dataType: 'html',
success: function(data) {}
})
}, 1500);
Answer the question
In order to leave comments, you need to log in
You need to check for new messages by interval.
let lastMessageId = null
let checkNewMessageInterval = setInterval(() => {
$.ajax({
url: 'ajax/checkNewMessage.php',
type: 'POST',
cache: false,
data: {last_id: lastMessageId},
dataType: 'html',
success: function(response) {
let lastMessage = response.data[response.data.length - 1]
lastMessageId = lastMessage.message_id // сохраняем последнее сообщение которое получили
// .....
}
})
// ...
}, 5000)
SELECT * FROM chat WHERE `message_id` > $_POST['last_id']
SELECT COUNT(*) FROM chat WHERE `message_id` > $_POST['last_id']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question