How to get more records or data on scroll down of page in PHP like facebook?

Do you know How to display more records while scrolling the page down using JQuery and PHP?

If you are familiar with jQuery and PHP, then it is not hard nut to crack.

Have a look on below code and you can do the same. Include JQuery library File:

Include JQuery library File:
<script type=’text/javascript’ src=’http://code.jquery.com/jquery-1.7.2.js’></script>

 

Create or call using a simple jQuery function in JS file (If you want to make the code separate).

[code lang=”js”]

$(window).scroll(function(){
scrollFunc();
});

function scrollFunc()
{
if($(window).scrollTop() == ($(document).height() – $(window).height())){

var offset = $(‘[id^=&quot;dataRow_&quot;]’).length;
var records = $(&quot;.totNum&quot;).text();

$(window).unbind(&quot;scroll&quot;);

if(records != offset){

$(&quot;#loaderImg&quot;).html(‘&lt;img src=&quot;images/ajax-loader.gif&quot; /&gt;’);
loadMoreRecords(offset);

}

}

}

function loadMoreRecords(offset){

// here we call ajax to add more records in our page.
$.ajax({

type: ‘get’,
url: ‘loadMyPageRecords.php’,
data: ‘offset=’+offset,

success: function(data){

$(&quot;#loaderImg&quot;).html();
$(&quot;.loadData :last&quot;).after(data);

},

error: function(data){
alert(&quot;ajax response error…&quot;+data);
}

}).done(function(){

$(window).bind(&quot;scroll&quot;,function(){
scrollFunc();

});
});

}
[/code]

 

How simple !! Isn’t it?

If you still face any issue, post your comments.