滚动不起作用时加载内容



我正在编写一段代码,我将其滚动到窗口底部,它在php代码的帮助下加载另一个数据。它加载load_first.php文件,但不标识load_second.php文件这个代码中有什么问题?请参阅这个

  <?php $_SESSION['first']=1; if($_SESSION['first']==1) { ?>
<script type="text/javascript">
    $(window).scroll(function(){
        if($(window).scrollTop() == $(document).height() - $(window).height()){
            $('div#last_msg_loader').html('<img src="loading.gif">');
            var data="cachekey=" + $('.cachekey').val()+"&cachelocation="+ $('.cachelocation').val();
            $.ajax({
                url: "load_data.php?last_msg_id="+ID,
                data:data,
                error:function(error)
                {
                    alert(error);
                },
                success: function(html){
                    if(html){
                        $(".message_box:last").after(data); 
                        $('div#last_msg_loader').hide();
                    }else{
                        $('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
                    }
                }
            });
        }
    });
</script>
<?php
include('load_first.php');
?>

<?php
}
else
{
  include('load_second.php');       
    }
    ?>  
    <div id="last_msg_loader"></div>

实际上,此代码用于使用expedia 的xmlapi加载酒店列表

您在if/else逻辑中犯了一个错误。首先,您设置:

$_SESSION['first']=1;

然后你直接跟着if:

if($_SESSION['first']==1) {

这意味着这总是正确的。其他条款:

else
{
   include('load_second.php');
}

因此从不触发。这等同于写作:

if (TRUE) {
} else {
    # PHP will never go here.
}

因此,无论您想对$_SESSION变量做什么,在任何情况下都不能使其包含load_second.php

您假设这个条件在滚动的底部为true

  if($(window).scrollTop() == $(document).height() - $(window).height())

使用控制台记录您将看到的值,它们永远不会完全相等

最新更新