简体   繁体   中英

Passing URL variable through jquery to another page

Hi i am using a load more script to load more content on the same page when clicked on load more. Here is the script...

<script type="text/javascript">
    var track_page = 1;
    load_contents(track_page);

    $("#load_more_button").click(function (e) { 
        track_page++; 
        load_contents(track_page);
    });

    function load_contents(track_page){
        $('.animation_image').show();

        $.post( 'fetch_pages.php', {'page': track_page}, function(data){

            if(data.trim().length == 0){
                $("#load_more_button").text("You have reached end of the record!").prop("disabled", true);
            }

            $("#results").append(data);

            $('.animation_image').hide();
        });
    }
</script>

INDEX.PHP

$_SESSION['name']=$_GET['name'];
$_SESSION['area']=$_GET['area'];

<div class="wrapper">
    <div id="results"></div>

    <button id="load_more_button"><img src="ajax-loader.gif"  class="animation_image" style="float:left;"> Load More</button>
</div>

fetch_pages.php

include("db.php");
$item_per_page = 10;
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

$position = (($page_number-1) * $item_per_page);
$sort="id";
if(isset($_GET['sort'])&&($_GET['sort'])=='ASC'){
    $sort="price ASC";
}
elseif(isset($_GET['sort'])&&($_GET['sort'])=='DESC'){
    $sort="price DESC";
}
if($_SESSION['area']=='All'){
    $stmt = $mysqli->prepare("SELECT * from table where name= ? ORDER BY $sort LIMIT ?,?");
    $stmt->bind_param("sdd", $_SESSION['name'], $position, $item_per_page);
}
else{
    $stmt = $mysqli->prepare("SELECT * from table where name= ? AND area= ? ORDER BY $sort LIMIT ?,?");
    $stmt->bind_param("ssdd", $_SESSION['name'], $_SESSION['area'], $position, $item_per_page);
}
    $stmt->execute();
    $i=1;
    $res = $stmt->get_result();
    while($row4=mysqli_fetch_array($res)){
       ---------------
       ---------------
}

The above script works fine if no GET variable is available in URL. But If i want to load page content on basis of $_GET variable it gives an error that $_GET variable not defined. Where i need to define these variables.. I also have tried defining S_SESSION variables on fetch_pages.php along with index.php but it still gives an error.

What is the best way to do this ? Is it possible to not to create fetch_pages.php separately and defining its content in index.php page ?

Thanks...

this my doubt you can check this- (accessing $_GET with out setting it), check first

if(isset($_GET['sort'])&&($_GET['sort'])=='ASC'){
    $sort="price ASC";
}
elseif(isset($_GET['sort'])&&($_GET['sort'])=='DESC'){
    $sort="price DESC";
}
// change  above code snippet to following code
$sort ='';
if(isset($_GET['sort']))
{
  // if sort is not ASC then its definitely DESC 
  $sort = ($_GET['sort']=='ASC')?"price ASC" : "price DESC";
}

Now for second code snippet

    $area = '%%';
    $name = '%%';
    if(isset($_GET['area'])){
        $area = ($_GET['area']=='All' || empty($_GET['area']))?'%%':'%'.$_GET['area'].'%';
     }
    if(isset($_GET['name']))
    {
        $name = ($_GET['name'])?'%'.$_GET['name']:'%%';
    }
    $stmt = $mysqli->prepare("SELECT * from table where name like ? AND area like ? ORDER BY $sort LIMIT ?,?");
    $stmt->bind_param("ssdd", $name , $area, $position, $item_per_page);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM