javascript - How GET works with AJAX -


i have ajax pagination working, not work better, i'm trying understand how ajax works url pagination work correctly.

what happens in normal pagination, without ajax each of links worked normal, more prev , next: prev 1 2 3 4 next, since get in same url.

but ajax sequence of numbers works. if put $next = $current_page + 1; $current_page variable receives current url without value of $_get["page_no"] ajax if i'm on page 8 , click next on pagination, apparently not recognizing in current url, next goes page 1 instead of page 9.

i not know if has working date-haref, rather href.

thanks

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 )                         }    }); }) 

before ajax call need add current page number onto url query variable, becomes like:

http://my-server.domain/path?page_no=4 

that last bit picked $_get in php. in javascript you'll need way detect current page number , add onto clicked url.

p.s. recommend using library urijs manipulate urls, rather pasting onto end.


Comments