-3

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&currentPage=1

if I click on number 8

currentPage=8&currentPage=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
            }
        }
Gislef
  • 1,555
  • 3
  • 14
  • 37

2 Answers2

0

$_GET variable will give you only what is exposed on the called url like: - http://yourdomain.com?someParameter=SomeValue&anotherParameter=AnotherValue

Will be:

Array(
   someParameter => SomeValue
   anotherParameter => AnotherValue
}

Essentially your mistake was to declare currentPage on your url and on your data properties. There is no rule that forbid to have the same parameter twice on the url, actually this is common in certain frameworks. As it seams it was not your intention so you should just remove the currentPage:1 from your data property on ajax call.

But for the knowledge, ajax will be no different from a normal form submit, besides the fact that it doesn't require page reload to do so. So you can follow normal behavior from a common form (post or get) and how to get the info on your php code.

You can make the ajax request like this:

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

   var url = $(this).data('href');

   $.ajax({
         method:"GET",
         url: url,
         success: function (response) {

           var html = $('<h1/>', {html : response}).find('#paginacao-ajax');
           $('#paginacao-ajax').html( html )    


         }
   });
})

PHP, by default when using $_GET or $_POST variables, will give you only the value of the last parameter defined with the same name.

So if you define tree times the parameter A for example (A=1&A=2&A=3) accessing it with $_GET['A'] will give you result 3.

If you wish to have all the values regarding parameter A you will have to parse the url your self.

Using $_SERVER['REQUEST_URI'] you have access to the url string and can easily explode by "&", and than for each one explodes again by "=" , than create an array with the values from the parameters with the same name. Or any other solution like summing, or whatever is more suitable for you.

Guilherme Ferreira
  • 2,209
  • 21
  • 23
  • Thanks Guilherme, but as you exemplified it was the first way I tried. It happens that using this same code when clicking on each of the link in console tab does not appear error, in the network tab is 200 ok but in any link I click the url is `unidade.php` . Get apparently does not work – Gislef Sep 12 '17 at 23:38
  • That's because you're using type: "get" where it should be method:"GET". :) – Guilherme Ferreira Sep 13 '17 at 15:08
0

In your js code replace currentPage=1 for currentPage=$(this).attr('data-href')

See this sample:

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

   var url = $(this).attr('href');
   var page = $(this).attr('data-href');

   $.ajax({
         type:"get",
         url: url,
         data: {
            currentPage: page
         },  
         success: function (response) {

           var html = $('<h1/>', {html : response}).find('#paginacao-ajax');
           $('#paginacao-ajax').html( html )    


         }
   });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class='page-item'><a href='#' class='page-link' data-href='1'>Pag 1</a></li>
<li class='page-item'><a href='#' class='page-link' data-href='2'>Pag 2</a></li>
<li class='page-item'><a href='#' class='page-link' data-href='3'>Pag 3</a></li>
<ul>
  • Thanks Jean and @Omi. Clicking on each link is displaying in the console the clicked link e.g: `currentPage=9`. But is not displaying the content of each page. And apparently this is still not getting value using `$ _GET` How does the server capture the value of `$ _GET` working with `AJAX` if the data value does not appear in the url? – Gislef Sep 11 '17 at 19:37
  • Are you tracking the content being sent via ajax where? Can you make a movie? – Jean Michél Marca Sep 11 '17 at 19:56
  • @Gislef just remove `currentPage=` from `data-href` it should be like `data-href='1'` `data-href='2'`... – Omi Sep 11 '17 at 19:58
  • @Gislef For monitoring ajax request you used console of chrome? https://stackoverflow.com/questions/1820927/request-monitoring-in-chrome – Jean Michél Marca Sep 11 '17 at 20:13
  • Jean please see, Console image: [link](http://tinypic.com/view.php?pic=w8xboy&s=9#.WbbyjsiGMdU) Class.crud.php: [link](https://github.com/GihGis/pagination/blob/master/class.crud.php) index.php [link](https://github.com/GihGis/pagination/blob/master/index.php) – Gislef Sep 11 '17 at 20:36
  • @Omi Thanks now I understood, I thought it was necessary to insert `currentPage=`. But with or without the result being the same. Appears on console "1", "2"... but pagination does not work – Gislef Sep 11 '17 at 20:41
  • @Gislef The problem is now on the page being called by ajax. In the url use the domain of your server / page that will be called. Example: var url = 'http://localhost/test.php'; – Jean Michél Marca Sep 11 '17 at 20:42
  • Jean I added `$self = $_SERVER['PHP_SELF'];` and `data-href='".$self."".$i."'` the result is the same, only the numbers appear on the console but the pagination does not work – Gislef Sep 11 '17 at 21:05
  • Place the page you want to send fixedly to test first, see that the error you sent in print is 404 status, ie page not found. The problem is in the link you are putting to url and not in the parameter you are sending. – Jean Michél Marca Sep 11 '17 at 21:14
  • I'm adding the full link manually, and even then the error is 404 – Gislef Sep 11 '17 at 21:25
  • Access the page in request ajax on URL of your browser, that you see page of error – Jean Michél Marca Sep 11 '17 at 21:58
  • I followed his guidance and corrected the url, now it's 200 ok, but ajax paging still does not work, any clicked link appears on the console but does not change on the screen. Image: [link](http://tinypic.com/view.php?pic=e999he&s=9#.Wbcd8tFv8dU) – Gislef Sep 11 '17 at 23:40
  • try `$(this).attr('data-href')` for get page data, i modify the sample code – Jean Michél Marca Sep 12 '17 at 01:22
  • see my test with sample code in this page. http://tinypic.com/r/fd94l5/9 – Jean Michél Marca Sep 12 '17 at 01:37
  • this corrected the url, now appears `currentPage=` but the problem with content display still continues [Link](http://tinypic.com/view.php?pic=2jbkrxe&s=9#.Wbc5ttFv8dU) – Gislef Sep 12 '17 at 01:38
  • Yes the request appears ok in `Network`, but clicking on the links the contents of each button did not display, always remains in the first content (button 1) – Gislef Sep 12 '17 at 01:41
  • what this show response request? send print please. – Jean Michél Marca Sep 12 '17 at 01:44
  • I copied the code that appears in the response tab [Link](https://github.com/GihGis/pagination/blob/master/response%20request) – Gislef Sep 12 '17 at 02:06
  • `$('#paginacao-ajax').html( response )` loads the entire page inside the table. see image: [link](http://tinypic.com/view.php?pic=2s8nyvk&s=9#.WbiBz8iGMdU) so the top pagination the numbers is working, the numbers are changing according to the click, the black number is the response `echo $ _GET ["currentPage"]`, but the content above the pagination does not change. – Gislef Sep 13 '17 at 00:59
  • The pagination that is below, clicking on it changes the pagination numbers from above as well, but does not change the color and nor does the `echo $ _GET ["currentPage"] ` always print 1 – Gislef Sep 13 '17 at 01:03
  • so I understand, you're returning the entire page in your ajax response, the ideal would be to return only the parts you want to update. Now coming back to your question, try swapping the entire page content in ajax return. try this in your success of ajax: `$('html').html( response );` or `$('body').html( response );` – Jean Michél Marca Sep 13 '17 at 02:36