2013-07-16 26 views
0

我想在我的文章中使用AJAX放置無限滾動,並且它沒有從PHP頁面獲取任何內容。我正在使用PDO來訪問數據庫。我試過使用正常限制0,12,它加載罰款。AJAX沒有收到PHP內容

這是我迄今所做的:

chapter.php:

<?php 
    include("includes/init.php"); 
    $query=$db->prepare("SELECT * FROM `test_db` WHERE `test_db`.`Chapter` LIKE :chapter ORDER BY `id` ASC"); 
    $chapter= (isset($_GET['chapter']) === true) ? $chapter= htmlentities($_GET['chapter']) : die(header('Location:..')); 
    $query->bindValue(':chapter', '%'. $chapter . '%', PDO::PARAM_STR); 
    $query->execute(); 
    $number= $query->rowCount();  
?> 
<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Document</title> 
</head> 
<body> 
<div class="container"> 
<ul> 
<script> 
    var page = 1; 
if($(window).scrollTop() + $(window).height() == $(document).height()) { 
    page++; 
    var actual_count = "<?php echo $number; ?>"; 
    if ((page-1)* 12 < actual_count){ 
     $.ajax({ 
      type: "POST", 
      url: "data.php", 
      data:{ 'page': page }, 
      success: function(res) {$(".container ul").append(res);} 
     }); 
    } 
} 
</script> 
</ul> 
</div> 
</body> 
</html> 

data.php:

<?php 
    include("includes/init.php"); 
    $page= $_POST['page']; 
    $articles = (($page- 1) * 12) . ", 12"; 
    $articles=$query->prepare("SELECT * FROM `test_db` WHERE `test_db`.`Chapter` LIKE :chapter ORDER BY `id` ASC LIMIT $articles"); 
    $chapter= (isset($_GET['chapter']) === true) ? $chapter= htmlentities($_GET['chapter']) : die(header('Location:..')); 
    $query->bindValue(':chapter', '%'. $chapter . '%', PDO::PARAM_STR);   
    $articles->execute(); 
    $rows= $articles->fetchAll(PDO::FETCH_ASSOC); 
    foreach ($rows as $row){ 
     echo "<li>".$row['Name']."-".$row['Description']."</li>"; 
    }; 
exit; 
?> 
+0

你不在'data.php'中調用'bindValue()'。 – Barmar

+0

@Barmar hmmm .. – tmanolescu

+0

'test)db'.'Chapter' typo –

回答

0

試試這個

var articles_count = "<?php echo $number; ?>"; 
    while ((page-1)* 12 < articles_count){ 
     $.ajax({ 
     type: "POST", 
     url: "data.php", 
     data:{ 'page': page }, 
     success: function(res) { 
     $(".container ul").append(res);} 
    }); 
    } 
+0

and remove var data = {page:page}; ? – tmanolescu

+0

是的。因爲這就是你正在做的數據:{'page':page}。所以刪除var data = {page:page}; –

+0

mmm ..現在我得到一些奇怪的數據回來:)我得到的索引頁.... – tmanolescu