使用AJAX post滚动来自MySQL的内容
我的网站有一部分需要在用户到达底部时动态加载内容。 我正在使用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("End of countries !!!!!!!
"); 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="" class="id">