Dynamically load data on div scroll using php, mysql, jquery and ajax

In this tutorial I’ll show you how can you dynamically load data on div scroll using php, mysql, jquery and ajax or you can say that facebook like paging, When ever user will be bottom of the div or page then new data will be loaded instantly.

In this example i have database of countries and i need to display all country list inside the div, So when ever user scroll country div next list of countries will be loaded.

First of all create countries database.

CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sortname` varchar(3) NOT NULL,
`name` varchar(150) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=247 ;

sload
Create php file to make connection with database and fetch countries list as per limit.

<?php
$hostname = "localhost";
$username = "root";
$password = "root";
$dbname = "location";
$limitStart = $_POST['limitStart'];
$limitCount = 50; // Set how much data you have to fetch on each request
	if(isset($limitStart ) || !empty($limitStart)) {
	$con = mysqli_connect($hostname, $username, $password, $dbname);
	$query = "SELECT id, name FROM countries ORDER BY name limit $limitStart, $limitCount";
	$result = mysqli_query($con, $query);
	$res = array();
		while($resultSet = mysqli_fetch_assoc($result)) {
		$res[$resultSet['id']] = $resultSet['name'];
		}
	echo json_encode($res);
	}
?>




Now create html file and put some css and html

<style>
.country { height: 300px; overflow: auto; width: 500px; }
.loading { color: red; }
li {font-size:24px;}
#loading { display:none; color:red; font-size:30px; }
</style>
<div class="country">
    <ul id="results"></ul>
</div>
 <span id="loading">Loading Please wait...</span>





After that write jquery to send request to server on div scroll

<script>
$(function() {
   loadResults(0);
    $('.country').scroll(function() {
      if($("#loading").css('display') == 'none') {
        if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
           var limitStart = $("#results li").length;
           loadResults(limitStart); 
        }
      }
	}); 
 
function loadResults(limitStart) {
	$("#loading").show();
    $.ajax({
        url: "request.php",
        type: "post",
        dataType: "json",
        data: {
            limitStart: limitStart
        },
        success: function(data) {
               $.each(data, function(index, value) {
               $("#results").append("<li id='"+index+"'>"+value+"</li>");
             });
             $("#loading").hide();     
        }
    });
};
});
</script>

Now your final index.html file will be

index.html

<style>
.country { height: 300px; overflow: auto; width: 500px; }
.loading { color: red; }
li {font-size:24px;}
#loading { display:none; color:red; font-size:30px; }
</style>
<div class="country">
    <ul id="results"></ul>
</div>
 <span id="loading">Loading Please wait...</span>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(function() {
   loadResults(0);
    $('.country').scroll(function() {
      if($("#loading").css('display') == 'none') {
        if($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
           var limitStart = $("#results li").length;
           loadResults(limitStart); 
        }
      }
	}); 
 
function loadResults(limitStart) {
	$("#loading").show();
    $.ajax({
        url: "request.php",
        type: "post",
        dataType: "json",
        data: {
            limitStart: limitStart
        },
        success: function(data) {
               $.each(data, function(index, value) {
               $("#results").append("<li id='"+index+"'>"+value+"</li>");
             });
             $("#loading").hide();     
        }
    });
};
});
</script>

See live demo and download source code.

DEMO DOWNLOAD

If you like this post please don’t forget to subscribe my public note book for more useful stuff