title: AJAX pagination, update current page
body: <p>I created a numeric pagination that loads the data via AJAX.</p>
  <p>This is the code:</p>
  <pre><code> $(document).ready(function(){  
        load_data(1); 
       
        function load_data(page)  
        {          
          $.ajax({
                  url:&quot;pagination2.php&quot;,  
                  method:&quot;POST&quot;, 
                  data:{page:page},  
                  success:function(data){  
                       $('#load_data').html(data);
                  } 
             }); 
        }  
  
        $(document).on('click', '.pagination_link', function(event) 
          {
              event.preventDefault(); 
              var page = $(this).attr(&quot;id&quot;); 
              load_data(page);
              //Now push PAGE to URL
              window.history.pushState({page}, `Selected: ${page}`, `${'page=' +  page}`)
              return event.preventDefault(); 
        });
              window.addEventListener('popstate', e =&gt; {
              var page = $(this).attr(&quot;id&quot;);
              load_data(e.state.page);
              console.log('popstate error!');
      });
      
   }); 
  </code></pre>
  <p>The code works, but when I refresh the page the data are loaded again from the first page. <a href="http://mantykora.cleoni.com:8080/2020_pagination_1/public/page=5" rel="nofollow noreferrer">test_ajax</a></p>
  <p>Ex: If I click the button that loads the page 2 when I refresh the page the data are displayed from the page 1.</p>
  <p>There is a way to remain on the same page even after the page refresh?</p>
  <p><strong>pagination2.php</strong></p>
  <pre><code>&lt;?php 
  
  $rowperpage = 10;
  
  $page = 1;
  if($_REQUEST['page'] &gt; 1)
  {
    $p = (($_REQUEST['page'] - 1) * $rowperpage);
    $page = $_REQUEST['page'];
  }
  else
  {
    $p = 0;
  }
  
  ?&gt;
  
  
  &lt;?php   
   $visible = $visible ?? true;
    
  $products_count = count_all_products(['visible' =&gt; $visible]);    
        
  $sql = &quot;SELECT * FROM products &quot;;
  $sql .= &quot;WHERE visible = true &quot;;
  $sql .= &quot;ORDER BY position ASC &quot;;
  $sql .= &quot;LIMIT &quot;.$p.&quot;, &quot;.$rowperpage.&quot;&quot;;  
          
  $product_set = find_by_sql($sql);
        
  $product_count = mysqli_num_rows($product_set);
      
  if($product_count == 0) {
        echo &quot;&lt;h1&gt;No products&lt;/h1&gt;&quot;;
        }
  
  while($run_product_set = mysqli_fetch_assoc($product_set)) { ?&gt;
  
      &lt;div style=&quot;float: left; margin-left: 20px; margin-top: 10px; class=&quot;small&quot;&gt;
  
        &lt;a href=&quot;&lt;?php echo url_for('/show.php?id=' . h(u($run_product_set['id'])));   ?&gt;&quot;&gt;
            &lt;img src=&quot;staff/images/&lt;?php echo h($run_product_set['filename']); ?&gt; &quot; width=&quot;150&quot;&gt;
          &lt;/a&gt;
  
        &lt;p&gt;&lt;?php echo h($run_product_set['prod_name']); ?&gt;&lt;/p&gt;
  
      &lt;/div&gt;
  
      &lt;?php }
  
      mysqli_free_result($product_set);
        
  ?&gt;
  &lt;section id=&quot;pagination&quot;&gt;
                             
      
  &lt;?php
      
  $url = url_for('index_all.php');
                          
  if($_REQUEST['page'] &gt; 1)
      { 
        $page_nb = $_REQUEST['page'];
      }
      else
      {
        $page_nb = 1;
      }                       
                              
  $check = $p + $rowperpage;
  $prev_page = $page_nb - 1;
  
  $limit = $products_count / $rowperpage;
      //echo $limit;
      //exit;
  $limit = ceil($limit);
  $current_page = $page_nb;
      
  if($page_nb &gt; 1) {
      //echo &quot;&lt;a href='index_all.php?page=&quot;.$prev_page.&quot;'&gt;Back&lt;/a&gt;&quot;;
      echo &quot;&lt;span class='pagination_link' style='cursor:pointer;'  id='&quot;.$prev_page.&quot;'&gt;Back&lt;/span&gt;&quot;;
  
  }
      
  if ( $products_count &gt; $check ) {
  
      for ( $i = max( 1, $page_nb - 5 ); $i &lt;= min( $page_nb + 5, $limit ); $i++ ) {
          if ( $current_page == $i ) {
              echo &quot;&lt;span class=\&quot;selected\&quot;&gt;{$i}&lt;/span&gt;&quot;;
          } else {
              echo &quot;&lt;span class='pagination_link' style='cursor:pointer;' id='&quot;.$i.&quot;'&gt;&quot;.$i.&quot;&lt;/span&gt;&quot;; 
          }
        }
      }       
      
  if ($products_count &gt; $check) { 
  $next_page = $page_nb + 1;  
      echo &quot;&lt;span class='pagination_link' style='cursor:pointer;' id='&quot;.$next_page.&quot;'&gt;Next&lt;/span&gt;&quot;;
      
  }
  </code></pre>