Reputation: 145
I know this question has been answered multiple times but I haven't been able to find one specifically to solve my problem.
I'm making a website where a user can sell their eBay items, using the eBay API (which is a bit of a pain!). My problem is at the results' pagination: I need a way of updating a $_SESSION
page number variable so that the page reloads and displays a different page of results. I'm doing this with AJAX, and it's my first time using it.
The process is the following:
1)User clicks on one of the pagination links on the bottom of the page (like what's at the bottom of your Google results), which I made into a form with a hidden field storing the next page number that updates accordingly when the user clicks.
2)Use AJAX to update said $_SESSION
variable on an external page (without refreshing!)
3)Reload the page and display the appropriate page of results.
The problem seems to be between steps #2 and 3. The bits of code I use:
Set $_SESSION variable
<?php
session_start();
if(!isset($_SESSION['pageNumber']))
{
$_SESSION['pageNumber'] = "1";
echo("Session value NOT set, pageNumber = " . $_SESSION['pageNumber']);
}
else
{
echo("Session value SET, pageNumber = " . $_SESSION['pageNumber']);
}
?>
Pagination
<form action="">
<div id="pagination" style="text-align:center; <?php if($totalPages == 1) echo('display:none;');?>">
<?php
global $totalPages;
if(intval($_SESSION['pageNumber']) != 1)
{
print("<a href=\"\" onclick=\"updatePageNumber(-1);\">Previous Page</a>");
}
for($i = 1; $i <= $totalPages; $i++)
{
print("<a href=\"\" onclick=\"updatePageNumber('$i');\">$i</a>");
print(" ");
}
if(intval($_SESSION['pageNumber']) != $totalPages)
{
print("<a href=\"\" onclick=\"updatePageNumber(0);\">Next Page</a>");
}
?>
</div>
<input type="hidden" name="nextPageNumber" id="nextPageNumber" value="<?php echo($_SESSION['pageNumber']);?>">
</form>
Update Hidden Field and $_SESSION Value
function updatePageNumber(newNumber)
{
//Update hidden field
if(newNumber == -1)
{
var num = document.getElementById('nextPageNumber').value;
num--;
document.getElementById('nextPageNumber').value = num;
}
else if(newNumber == 0)
{
var num = document.getElementById('nextPageNumber').value;
num++;
document.getElementById('nextPageNumber').value = num;
}
else
{
document.getElementById('nextPageNumber').value = newNumber;
}
//Update $_SESSION value with AJAX
var dataString = 'nextPageNumber='+ document.getElementById('nextPageNumber').value;
$.ajax(
{
url: "scripts/updatePageNumber.php",
type: "POST",
data: dataString,
dataType: "text",
success: function(text)
{
alert("Done! Response: " + text);
}
});
And the PHP file
<?php
if (isset($_POST['nextPageNumber']))
{
session_start();
die("Made it this far!");
$nextPageNumber = $_POST['nextPageNumber'];
$_SESSION['pageNumber'] = $nextPageNumber;
echo $nextPageNumber;
}
?>
Note: I don't get to the PHP file's die()
method, nor the alert with the AJAX's success (by the way, is that how to properly get the response text?)
I'd greatly appreciate any help at all, I've been stuck at this for quite a long time now!
Upvotes: 1
Views: 3729
Reputation: 2929
Your ajax call doesnt have nextPageNumber
in its data, so it isn't seen by your PHP script (not in $_POST array) your test for it fails.
(As a general comment, your question involves too many working parts that makes it not a good fit for a site like this, devoted to helping others with similar problems. Next time please isolate the very part of the code that is causing the problem. This is likely why you have no other answers yet - people are sick of getting downvoted when trying to help solve something so comprehensive.)
Upvotes: 2