2017-02-25 90 views
0

嗨,我正在使用加載更多的腳本來加載更多的內容在同一頁面上加載更多的內容時。這裏是腳本...通過jQuery將URL變量傳遞到另一頁

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

上述腳本工作正常,如果沒有GET變量可用URL。但是,如果我想要在$_GET變量的基礎上加載頁面內容,它會給出$_GET變量未定義的錯誤。在哪裏我需要定義這些變量..我也試着在fetch_pages.php上與index.php一起定義S_SESSION變量,但它仍然給出錯誤。

這樣做的最好方法是什麼?是否有可能不單獨創建fetch_pages.php並在index.php頁面中定義其內容?

謝謝...

+0

嘗試從「'頁面'中刪除單引號:' –

+0

既不會給出任何錯誤,也不會顯示任何內容... – Ashish

+0

您使用POST方法將數據發佈到'fetch_pages.php'並訪問我用'$ _GET'功能怎麼可能...你能顯示當前的網址嗎? –

回答

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); 
+0

你好感謝你的努力,但這個問題不僅對排序變量...的名字和地區變量也存在問題... – Ashish

+0

您想查詢什麼火是'name'和'address'在獲取IsNot運算規定方法 –

+0

如果我沒有提到fetch_pages.php中的S_GET變量,我會得到「Undefined Variable:_SESSION」...但是如果我在fetch_pages.php上提及S_SESSION ['name'] = S_GET ['name'],那麼我得到「未定義的索引:名稱」.. – Ashish