How to Load Data On Scrolling Page Down using jQuery and PHP | YourSite

How to Load Data On Scrolling Page Down using jQuery and PHP

Web Development • 379 views

In this blog you will learn How to Load Data On Scrolling Page Down using jQuery & PHP

Prerequisites Section:

You need to have knowledge of HTML, CSS or Bootstrap 5.

You need to have knowledge of JavaScript or jQuery. Because here we used jQuery.

You need to have knowledge of PHP and AJAX.

You need to have knowledge of MySQL database.

HTML & CSS Section:

In this section we will load data.

 
<div id="prependHere">

</div>
 

When we will load the data before loading this spinner will be show to the user.

 
<div class="container d-flex justify-content-center"> 
            <div class="spinner-border m-5 text-danger" id = "loading" role="status">
              <span class="visually-hidden">Loading...</span>
            </div>
 </div>
 

JavaScript section:

Code in the script section. Remember that you should add ajax and jQuery library file before using it.

 
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
// code for scroll data load start

var page_no = 1;
var isrunning = false;
showdata();
$(window).scroll(function(){
    if($(window).scrollTop()+$(window).height() > $(document).height()-50){
        if(!isrunning){
            showdata();
        }
    }
}); 

function showdata(){
    isrunning = true;
    $("#loading").show();
    var functionID = 'partial_fetch';
    $.post("ajax/userPosts.php",{page:page_no, functionID:functionID},(response)=>{
        if(response){
            $("#prependHere").append(response);
            $("#loading").hide();
            isrunning = false;
            page_no++;
        }else{
            $("#loading").hide(); 
        }
    });
}

// code for scroll data load end 
 

AJAX PHP Section:

You can write the below code to show data to user.

 
function partial_fetch(){
    sleep(2);
  $page = $_POST['page']??1;
  $limit = 5;
  $rowno = ($page - 1)*$limit;

  $query = "SELECT * FROM post limit $rowno, $limit";  
  $result = query($query, 'select'); 
  foreach($result as $key => $row){ ?>
      <div class="card">
      <div class="card-header">Post Header</div>
      <div class="card-body"> <?php echo $row['post_title']; ?> </div>
      <div class="card-footer">Post  Footer</div>
     </div>
     <?php
  }

}
 

🚀 More Blogs You Might Like

Explore more articles and keep learning

Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do
health
Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do

Heat Stroke in Summer: Causes, Symptoms, Prevention and What To Do...

👁 8 2026-04-26
Read More →
Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It
health
Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It

Alcoholic Fatty Liver Disease: Causes, Symptoms, Risks and How to Improve It...

👁 9 2026-04-26
Read More →
Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It
health
Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It

Non-Alcoholic Fatty Liver: Meaning, Causes, Symptoms, and How to Improve It...

👁 10 2026-04-26
Read More →
The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science
class-1-12-resources
The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science

The Ultimate Blueprint to Score 70/70 in ISC Class 12 Computer Science...

👁 47 2026-04-11
Read More →