I'm writing a web Chat system to permit user to write a message to their friend but I have a problem scrolling down the new messages using jquery.
I have these files: messages.php
where I store the form for submitting and view the messages;chatupd.js
executes post and get messages and refresh the div every 'x' seconds;post_updmsg.php
Prints messages to database; get_updmsg.php
Gets the latest messages from database;
But the only files needed for this question are the first two:
messages.php
<div class='chatContainer'>
<div class='chatHeader'>
<h3>Welcome <?php echo get_talkm3_nome(); ?></h3>
</div>
<div class='chatMessages'>
<?php
include '../core/config.inc.php';
$db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$id_usr_2 = $_SESSION['email'];
//Get Messages
$query = $db->prepare("SELECT * FROM `chat_t3`");
$query->execute();
//Fetch
while ($fetch = $query->fetch(PDO::FETCH_ASSOC))
{
$id_usr_2 = $fetch['id_usr_2'];
$id_usr_1 = $fetch['id_usr_1'];
$text = $fetch['text'];
$date = $fetch['date'];
if ($id_usr_1 == $_SESSION['email']){
echo "<li class='message_container'><div class='sender'>From - <b>".ucwords($id_usr_1)."</b> TO - ".ucwords($id_usr_2)."</b> - ".$text." ".ucwords($date)."</div></li> ";
} else {
echo "<li class='message_container'><div class='reader'>From - <b>".ucwords($id_usr_1)."</b> TO - ".ucwords($id_usr_2)."</b> - ".$text." -".ucwords($date)."</div></li> ";
}
}
?>
</div>
<div class='chatBottom'>
<form action='#' onSubmit='return false;' id='chatForm'>
<input type='hidden' id="id_usr_1" value="<?php echo get_talkm3_email() ?>"><br />
<input type='text' id='id_usr_2' value="" placeholder="inserisci la mail del destinatario" />
<input type='text' name='text' id='text' value='' placeholder='Type your message'>
<input type='submit' name='submit' value='Post'>
</form>
<br>
</div>
</div>
chatupd.js
$(function(){
$(document).on('submit', '#chatForm', function(){
var id_usr_1 = $.trim($("#id_usr_1").val());
var id_usr_2 = $.trim($("#id_usr_2").val());
var text = $.trim($("#text").val());
if (id_usr_1 != "" && id_usr_2 != "" && text != ""){
$.post('../core/chat/post_updmsg.php', {id_usr_1: id_usr_1, id_usr_2: id_usr_2, text: text}, function(data){
$(".chatMessages").append(data);
});
} else {
alert("Data Missing");
}
});
function getMessages (){
$.get('../core/chat/get_updmsg.php', function(data){
$(".chatMessages").html(data);
});
}
setInterval(function(){
getMessages();
}, 1000);
});
Now, my questions are:
- How can I automatically scroll down the page to display latest message? But if the user wants to see the old messages and scroll up the page, prevent that after 'x' seconds they is redirected to the bottom of the page?
- If the user scrolls up the page and script to scroll down is blocked, how to reactivate it when user scroll down completely the page?