【问题标题】:AJAX not receiving PHP contentAJAX 不接收 PHP 内容
【发布时间】:2013-07-16 10:54:38
【问题描述】:

我正在尝试使用 AJAX 对我的文章进行无限滚动,但它没有从 PHP 页面获取任何内容。我正在使用 PDO 访问数据库。我尝试使用正常限制 0、12 并且加载正常。

这是我到目前为止所做的:

章节.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>

数据.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;
?>

【问题讨论】:

  • 你没有在data.php中调用bindValue()
  • test)db.Chapter 错字

标签: php jquery ajax pdo


【解决方案1】:

试试这个

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

【讨论】:

  • 并删除 var data = { page: page }; ?
  • 是的。因为这就是你正在做的数据:{ 'page': page }。所以删除 var data = { page: page };
  • 那个奇怪的数据就是你从 php 脚本中回显的数据
  • 好的,但是 data.php 应该访问数据库,而 index.php 是静态的.. 那为什么要回显索引呢?
  • 好的,这就是为什么我得到 index.thml - : die(header('Location:..'));
猜你喜欢
  • 1970-01-01
  • 2016-03-12
  • 2022-12-13
  • 2020-08-08
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
相关资源
最近更新 更多