0
I have a pageant AJAX
that is working, actually without working better, I’m trying to understand how the AJAX
works with the url for paging to work properly.
What happens is that in normal pagination, without AJAX
each of the links functioned normal, more specifically the Prev and the next: Prev 1 2 3 4 next, since the GET
was at the same url.
But with AJAX
only the sequence of numbers works. If I put $next=$current_page+1;
the variable current_page always receives the current url without the value of $_GET["page_no"]
then with AJAX
if I am on page 8 and click next in the pagination, because apparently I am not recognizing the GET
in the current url, next goes to page 1 instead of page 9.
I don’t know if this has anything to do with me working with data-haref
, instead of href
.
I appreciate help
class.crud.php
public function paginglink($query,$records_per_page)
{
$self = $_SERVER['PHP_SELF'];
$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["page_no"]))
{
$current_page=$_GET["page_no"];
}
if($current_page!=1)
{
$previous =$current_page-1;
echo "<li class='page-item'><a href='#' class='page-link' data-href='".$self."?page_no=1'>First</a></li>";
echo "<li class='page-item'><a href='#' class='page-link' data-href='".$self."?page_no=".$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='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
}
else
{
echo "<li class='page-item'><ahref='#' class='page-link' data-href='".$self."?page_no=".$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='".$self."?page_no=".$next."'>Next</a></li>";
echo "<li class='page-item'><a href='#' class='page-link' data-href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
}
?></ul><?php
}
}
AJAX
$('.page-link').click(function (e) {
e.preventDefault();
var url = $(this).data('href');
$.ajax({
url: url,
success: function (response) {
var html = $('<h1/>', {html : response}).find('#paginacao-ajax');
$('#paginacao-ajax').html( html )
}
});
})
"I don’t know if this has anything to do with me working with data-haref instead of href, "I don’t see any
data-href
in the code you put.– Isac
@Isac updated the code
– Gislef
And the urls that are being called here
var url = $(this).data('href');
are correct ? make aconsole.log
of that amount to confirm– Isac
took out the
href="#"
I just leftdata-href
and what is being captured by the stretchvar url = $(this).data('href');
is thedata-href
– Gislef
@Isac on the console is giving ok to all pagination numbers ex:
/pagina.php?page_no=1
, but for next and Prev this giving/undefined
in the url and next to the error description is 404 not found,– Gislef
Looks like you’re already figuring out the problem. Check your
$current_page
has the correct value withvar_dump
or similar– Isac
var_dump
returnsint
,print_r retorna
1– Gislef
Before or after the
$current_page=$_GET["page_no"];
? You should test after that line to ensure that you are reading correct values from$_GET
. Also take and confirm the value of$next
and/or$previous
to ensure that they are right before reaching theecho
– Isac
@Isac tested before and after the result is the same
– Gislef