I'm a tad stumped here. I have 2 php pages. One displays the template essentially, and on click, an AJAX call calls an external page and displays all the necessary information. My problem is my session variable isn't updating. I'm using some pagination and the numbers aren't updating. Some code on my template page:
Redndering pagination
session_start();
$pagination .= $_SESSION['position'] == "" ? '' : 'Items '.$_SESSION['position'].'-'.$_SESSION['item_per_page'].' of '.$get_total_rows ;
if($pages > 1)
{
$pagination .= '<ul class="paginate nav nav-pills">';
for($i = 1; $i<=$pages; $i++)
{
$pagination .= '<li><a href="#" class="paginate_click" id="'.$i.'-page">'.$i.'</a></li>';
}
$pagination .= '</ul>';
}
<?php echo $pagination; ?>
<div id="results">
<!-- Results from query will go in here -->
</div>
j$(".paginate_click").click(function (e) {
j$("#results").prepend('<div class="loading-indication"><img src="/assets/loader.gif" /></div>');
var clicked_id = j$(this).attr("id").split("-"); //ID of clicked element, split() to get page number.
var page_num = parseInt(clicked_id[0]); //clicked_id[0] holds the page number we need
j$('.paginate_click').removeClass('active'); //remove any active class
//post page number and load returned data into result element
//notice (page_num-1), subtract 1 to get actual starting point
j$("#results").load("development_fetch.php", {'page': (page_num-1)}, function(){
});
j$(this).addClass('active'); //add active class to currently clicked element
return false; //prevent going to herf link
});
in development_fetch.php:
session_start();
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
$position = ($page_number * $item_per_page);
$_SESSION['position'] = $position;
$_SESSION['item_per_page'] = $item_per_page;
//Grab code to display inside template blah.
If you click on page 1 in the pagination, it should result in:
Items 0 - 100 of 1000
If I click page 2, it should be:
Items 100 - 200 of 1000
but instead it just stays at:
Items 0 - 100 of 1000
0 is the $_SESSION['position']
100 is the $_SESSION['item_per_page']
I don't understand why it isn't updating. Please help stackers!
Put session_start();
at the begining of the page loaded with AJAX.
i think i found it, in your development_fetch.php
$position = ($page_number * $item_per_page);
okay, $page_number
has a value, but what about $item_per_page
? i guess its zero, and the result of multiplication of course is zero. Then you set those zeroes on the $_SESSION
solution:
set your $item_per_page
session_start();
$item_per_page = 20;
// ...rest of code
i also got this issue, but mine was acting weird. it seems that the session was not fast enough to catch with the ajax.. is this problem already resolved?
i already solved mine. the issue i'm having is that, when i'm doing the ajax, the session variable is not updating in realtime. what i did, is that, i just turned the ajax asynchronous to false, since when you are doing the asynchronous, the value being cache is not the updated value since it is in asynchronous mode. if you are using jquery, just do this:
$.ajax({ type: 'POST', url: 'url', data: 'the datas', success: function() {}, async: false });