通过jquery将URL变量传递到另一个页面

时间:2017-02-25 09:07:04

标签: javascript php jquery ajax

您好我正在使用加载更多脚本在点击加载更多内容时在同一页面上加载更多内容。这是脚本......

<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)){
       ---------------
       ---------------
}

如果URL中没有可用的GET变量,则上述脚本可以正常工作。但是,如果我想基于$_GET变量加载页面内容,则会出现$_GET变量未定义的错误。我需要定义这些变量。我也尝试在S_SESSIONfetch_pages.php上定义index.php个变量,但它仍然会出错。

最好的方法是什么?是否可以不单独创建fetch_pages.php并在index.php页面中定义其内容?

...谢谢

1 个答案:

答案 0 :(得分:0)

我怀疑你可以检查一下 - (在没有设置的情况下访问$ _GET),先检查

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";
}

现在是第二个代码段

    $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);