How to Load Data On Scrolling Page Down using jQuery and PHP
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.
When we will load the data before loading this spinner will be show to the user.
Loading...
JavaScript section:
Code in the script section. Remember that you should add ajax and jQuery library file before using it.
// 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){ ?> Post Header