dimanche 4 janvier 2015

Passing a page number from one PHP file to another using a JavaScript function


Vote count:

0




I'm working on a website with the following URL structure:


I'm trying to pass the current page number from index.php to myphpscript.php using a javascript function:



<script>
function swapContent(filterchoice){
var PageNum = <?php echo json_encode($_GET["page"]); ?>;
var url = "myphpscript.php";
$.post(url, {contentVar: filterchoice, page : PageNum}, function(data){
$("#myDiv").html(data).show();
});
}
</script>


On myphpscript.php, when adding the following condition for the http://ift.tt/tv1JJF case to get $page_number=1:



if (isset($_POST['page'])) {
$page_number = $_POST["page"];
}
else {
$page_number = 1;
}
echo $page_number;


It doesn't seem to echo anything for the default home page but echo the proper page number for all the other page with page= in the URL. However, the isset condition seems to work properly on index.php even for the default home page with:



if (isset($_GET["page"])) {
$page_number = $_GET["page"];
}
else {
$page_number = 1;
}


Could you explain me why it works for the second case and not the first one and how I can set $page_number=1 in myphpscript.php if I'm on the default home page.



asked 1 min ago







Passing a page number from one PHP file to another using a JavaScript function

Aucun commentaire:

Enregistrer un commentaire