I've been studying the jquery documentation and about ajax, I'm trying to understand how ajax and php requests work.
I'm trying to turn a normal paging into ajax paging.
How do I use the ajax data
and work with it on the server?
The way I tried this one returning in the console like this:
if I click on number 3 for example
currentPage=3¤tPage=1
if I click on number 8
currentPage=8¤tPage=1
and the screen does not change it stays in the same content
AJAX
$('.page-link').click(function (e) {
e.preventDefault();
var url = $(this).data('href');
$.ajax({
type:"get",
url: url,
data: {
currentPage: 1
},
success: function (response) {
var html = $('<h1/>', {html : response}).find('#paginacao-ajax');
$('#paginacao-ajax').html( html )
}
});
})
class.crud.php
public function paginglink($query,$records_per_page)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
$total_no_of_records = $stmt->rowCount();
if($total_no_of_records > 0)
{
?><ul class="pagination"><?php
$total_no_of_pages=ceil($total_no_of_records/$records_per_page);
$current_page=1;
if(isset($_GET["currentPage"]))
{
$current_page=$_GET["currentPage"];
}
if($current_page!=1)
{
$previous =$current_page-1;
echo "<li class='page-item'><a href='#' class='page-link' data-href='currentPage=1'>First</a></li>";
echo "<li class='page-item'><a href='#' class='page-link' data-href='currentPage=".$previous."'>Back</a></li>";
}
for($i=1;$i<=$total_no_of_pages;$i++)
{
if($i==$current_page)
{
echo "<li class='page-item'><a href='#' class='page-link' data-href='currentPage=".$i."' style='color:red;'>".$i."</a></li>";
}
else
{
echo "<li class='page-item'><ahref='#' class='page-link' data-href='currentPage=".$i."'>".$i."</a></li>";
}
}
if($current_page!=$total_no_of_pages)
{
$next=$current_page+1;
echo "<li class='page-item'><a href='#' class='page-link' data-href='currentPage=".$next."'>Next</a></li>";
echo "<li class='page-item'><a href='#' class='page-link' data-href='currentPage=".$total_no_of_pages."'>Last</a></li>";
}
?></ul><?php
}
}