WhatsApp us

Pagination using AJAX in PHP and MySQL

  • Tech Area
  • Last updated on: January 8, 2026



In this tutorial, We will learn how to make pagination using Jquery AJAX in PHP and MySQL. We are using numeric pagination to sort data from mysql database using AJAX.

Files used in this tutorial:

1- connection.php (database connection file)

2- index.php (table and AJAX script)

3- pagination.php (make dynamic pagination)

Below are the step by step process of how to make pagination Jquery AJAX.

Step 1: Create a Database connection

In this step, create a new file connection.php to create database connection.

connection.php

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "college_db";
$connection = mysqli_connect("$server","$username","$password");
$select_db = mysqli_select_db($connection, $database);
if(!$select_db)
{
	echo("connection terminated");
}
?>

Step 2: Create index.php

In this step, create a new file index.php. This is the main file used to implement AJAX to make pagination.

This screenshot shows the pagination.

index.php

<html>  
<head>  
    <title>AJAX Pagination</title>  
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"/>  
    <script src="https://code.jquery.com/jquery-3.7.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
</head>
<body>  
    <div class="container">  
    <h2 class="text-center">AJAX Pagination using Jquery AJAX</h2>
    <br/>
    <div id="result">
    </div>
  </div>
 </body>  
</html>

Now use jquery AJAX script.

<script type="text/javascript">
$(document).ready(function(){
   showdata();
 }); 
function showdata(page)
{
  $.ajax({
      url: 'pagination.php',
      method: 'post',
      data: {page_no:page},
      success: function(result)
      {
        $("#result").html(result);
      }
    });
}
$(document).on("click",".pagination a", function(){
var page = $(this).attr('id');
showdata(page);
});
</script>

Step 3: Create new file for pagination

In this step, create a new file pagination.php. This file used to make dynamic numeric pagination.

pagination.php

<?php
include("connection.php");
$limit_page = 3;
$page="";
if(isset($_POST['page_no']))
{
       $page = $_POST['page_no'];
}
else
{
       $page = 1;
}

$offset = ($page-1)* $limit_page;

$fetch_query = mysqli_query($connection, "select * from tbl_registration limit $offset, $limit_page");
$output="";
$row = mysqli_num_rows($fetch_query);
if($row>0)
{
       $output .='<table class="table table-bordered table-striped">
    <thead style="background-color:#e3a53a;">
      <th>Id</th>
      <th>Name</th>
      <th>Email</th>
      <th>Category</th>
      <th>Phone</th>
      <th>Address</th>
      </thead>';
    
   
	while($res = mysqli_fetch_array($fetch_query))
	{
       $output .="<tr>
       	<td>{$res['id']}</td>
       	<td>{$res['name']}</td>
       	<td>{$res['email']}</td>
              <td>{$res['category']}</td>
       	<td>{$res['phone']}</td>
       	<td>{$res['address']}</td>
       	
       </tr>";
       }
       $output .="</table>";

$fetch_query = mysqli_query($connection,"select * from tbl_registration");
$row = mysqli_num_rows($fetch_query);
$total_page = ceil($row/$limit_page);
       $output .='<ul class="pagination">';
for($i=1; $i<=$total_page; $i++)
{
       if($i==$page)
       {
              $active ="active";
       }
       else
       {
              $active = "";
       }
   $output .= "<li class='{$active}'><a id='{$i}'>{$i}</a></li>";
}  
  $output .='</ul>';

  echo $output;
}
?>

Download Source Code


Subscribe us via Email

Join 20,000+ subscriber

Subscribe on YouTube

PHP Projects
Matrimonial Portal Project in PHP & MySQL Last Updated: December 22, 2025
Event Management System Project in PHP & MySQL Last Updated: December 6, 2025
Online Shopping System Project in PHP MySQL Last Updated: November 26, 2025
Hostel management system project in PHP and MySQL Last Updated: February 14, 2024
Online Pizza Delivery project in PHP Last Updated: February 4, 2024
Parking Management System project in PHP Last Updated: November 5, 2023
Visitors Management System project in PHP Last Updated: August 28, 2023
SNIPE – IT Asset management system v6.1.0 Last Updated: April 21, 2023
Employee Management System project in PHP Last Updated: January 21, 2023