【问题标题】:Load content from MySQL on scroll with AJAX post使用 AJAX 帖子在滚动时从 MySQL 加载内容
【发布时间】:2012-06-28 01:40:27
【问题描述】:

我的网站有一段需要在用户到达底部时动态加载内容。我正在使用 jQuery,这是检测滚动的代码:

$(document).ready(function() { 
    $(window).scroll(function() {
        if($(window).scrollTop() + $(window).height() == $(document).height()) {
        alert("Bottom reached");
            $('div#loadMoreComments').show();
            dataStr = "from=" + $(".n:last").attr('id')
            $.ajax({
                type: "POST",
                url: "ajax/query.php",
                data: dataStr,
                success: function(html) {
                    if(html){       
                        $("#hold").append(html);
                        alert("Data was added");
                    }
                    else{       
                        $('div#loadMoreComments').replaceWith("<center><h1 style='color:red'>End of countries !!!!!!!</h1></center>");
                        alert("Data was not added");
                    }
                }
            });
        }
    });
});

我遇到的第一个问题是只有在用户到达页面顶部时才会检测到滚动到底部。第二个问题是它根本没有加载任何内容,因为变量似乎没有发布,这是我在query.php中的代码:

if(array_key_exists('from', $_POST)) {
    $from = htmlspecialchars(stripslashes(mysql_real_escape_string($_POST['from'])));
    $to = 15;
    $re = ("SELECT status as status, sid as sid, UNIX_TIMESTAMP(timestamp) as timestamp FROM mingle_status WHERE uid = '$uid' ORDER BY timestamp DESC LIMIT $from,$to"); //query
  }
  else {
    $re = ("SELECT id as id, status as status, sid as sid, UNIX_TIMESTAMP(timestamp) as timestamp FROM mingle_status WHERE uid = '$uid' ORDER BY timestamp DESC LIMIT 1"); //query
  }
  $result = mysql_query($re) or die (mysql_error());
  while($st = mysql_fetch_assoc($result)) {
    $status = nl2br($st['status']);
    $sid = $st['sid'];  
    $td = $st['timestamp'];
    $id = $st['id'];
    ?>
        <div id="<?php echo $id; ?>" class="id">
            <!-- stuff -->
        </div>
    <?php
    }
    ?>

还有错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '",15' at line 1

如果有人能帮我解决这个问题,那就太好了,我将不胜感激。

编辑:好的,我现在可以生成一个 div,但只有当我滚动到页面顶部时,它只会附加一个 div,如果我再次滚动到顶部,它附加了完全相同的 div。

【问题讨论】:

  • dataStr 是否正确创建?当您将 print_r($_POST) 添加到 php 到要发布到 query.php 的内容时会发生什么?
  • 是来自字符串或数字的 $ - 看起来像一个倒逗号被添加到 sql 搞砸了限制

标签: php jquery mysql ajax


【解决方案1】:

这是错误的:

$from = htmlspecialchars(stripslashes(mysql_real_escape_string($_POST['from'])));

如果from 应该是一个整数,只需使用:

$from = (int) $_POST['from'];

我还看到该数字来自 html 中的 id,而 ids 不能以数字开头。

编辑: 另一个问题是,如果 from 存在,您没有在 sql 查询中选择 ID,即使您这样做,这种方法可能会在将来导致问题您删除了记录,您的 ID 不再连续。

关于第一个问题,我可以在firebug更改中解决:

 if($(window).scrollTop() + $(window).height() == $(document).height()) {

到:

 if( ($(window).scrollTop() + $(window).height()) > ($(document).height() -  10) ) {

编辑 2:要解决您的非顺序 ID 问题,最简单的方法是在 javascript 中使用类似的方法计算 from

dataStr = "from=" + $(".n").length;    // just count the number of elements you are showing already

【讨论】:

  • 这解决了我的第二个问题,但它创建了两个新问题,1)它只在#hold 的底部添加一个 div,2)当我再次滚动时,它会附加相同的 div...
  • @AviateX14 生成的html是什么样子的?
  • 应该,所有的 id 都被放在正确的位置,但是 .n 的 id 是数据库中的第一个条目,它应该是 92(滚动之前它上面的那个是93)
  • 它现在不断地触发 ajax
  • @AviateX14 只需使用诸如 firebug 之类的东西并使用这些值来查看最适合您的值。
猜你喜欢
  • 2011-11-23
  • 2013-07-08
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 2012-04-04
  • 1970-01-01
相关资源
最近更新 更多