Ajax data - Paging

0

I've been studying 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.

More specifically, I use ajax data and work with it on the server.

As I tried, the console is returning like this (the option to embed OS image is not loading images or adding external links):

link

Clicking on each of the page numbers will display the status 200 ok on the console, and the corresponding number of each link, but nothing changes on the screen is always in the content of the first paging number.

AJAX

$('.page-link').click(function (e) {
   e.preventDefault();

   var url = "unidade.php"
   var page = $(this).attr('href');

   $.ajax({
         type:"get",
         url: url,
         data: {
            currentPage: page
         },  
         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  class='page-link' href='1'>First</a></li>";
                echo "<li class='page-item'><a class='page-link' href='".$previous."'>Back</a></li>";

            }
            for($i=1;$i<=$total_no_of_pages;$i++)
            {
                if($i==$current_page)
                {
                    echo $current_page;
                    echo "<li class='page-item'><a  class='page-link' href='".$i."' style='color:red;'>".$i."</a></li>";
                }
                else
                {
                    echo "<li class='page-item'><a class='page-link' href='".$i."'>".$i."</a></li>";
                }
            }
            if($current_page!=$total_no_of_pages)
            {

                $next=$current_page+1;
                echo "<li class='page-item'><a class='page-link' href='".$next."'>Next</a></li>";
                echo "<li class='page-item'><a  class='page-link' href='".$total_no_of_pages."'>Last</a></li>";
            }
            ?></ul><?php
        }
    }

Thank you for your help

    
asked by anonymous 12.09.2017 / 02:43

1 answer

0

What your ajax is doing is calling the unidade.php page and passing the variable currentPage as a parameter, if you do not have a call on page unidade.php page code class.crud.php will never be executed.

    
12.09.2017 / 15:12