I am trying to implement a functionality which loads more data from the database when the user reaches the end of a 'div', similar to twitter. I have a 'div' called 'userposts_panel' which consists of several other divs which are used to display content from the database. I.e.
<div id="userposts_panel">
<?php echo "<br/>
<div class='new_msg'>
<div class='user_dp'>
<img src='$profile_pic' width= '65px' height= '68px'/>
</div>
<div class='msg_content'>
<span>$message_content </span> <br/> <br/>
</div>
<div class='user_details'>
<span>Posted by: </span>
<a href='$thoughts_by'> <b> $name_of_user </b> </a> on $date_of_msg
</div>
</div><br/>"; ?>
</div>
I have the following JavaScript defined which obtains all mathematical data, such as when the user has reached the end of the page etc. But I am unable to figure out how I am suppose to get new data from the database and post it on the same page (see if statement). At the moment I have a query which displays 10 posts from the database on the page - LIMIT 1
, and when the user scrolls to the bottom of userposts_panel
, I need it to display the userposts_panel
which 10 new posts in addition to the ones which are already displayed.
function yHandler (){
var userposts_panel = document.getElementById('userposts_panel');
var contentHeight = userposts_panel.offsetHeight; // get page height
var yOffset = window.pageYoffset;// get vertical scroll position - find out where user is on scroll.
var y = yOffset + window.innerHeight;
if (y >=contentHeight){ // if the user scroll to the very bottom, do this..
userposts_panel.innerHTML += '<div class="userposts_panel"></div>'
// AJAX call to get more dynamic data here...
}
}
// event listner
window.onscroll = yHandler;
Do I have to create a new div rather than using the userposts_panel
to display new content as it already exists? And how do I get it to load data from the database in the div using AJAX?