PHP MYSQLI AJAX滚动加载不起作用

问题描述 投票:0回答:1

我有用于在单击按钮时加载更多内容的代码。我还有另一个可以滚动加载。按钮的代码加载正常。滚动代码将新加载的内容复制了3次。这两个代码使用相同的php加载文件。所有这些都在下面列出。滚动代码有什么问题?感谢所有帮助。仅供参考,我对Jquery无限滚动或任何其他插件不感兴趣。谢谢!


点击按钮加载


    <!DOCTYPE html>
    <html>

    <head>
        <title>Load More Button</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    </head>

    <body>
        <div class="container">
            <div>
                <h2 align="center">Load More Button</h2>
                <div style="height:10px;"></div>
                <div id="load-content">
                    <?php
                    $lastid = '';
                    include('connection.php');
                    $query = mysqli_query($connection, "select * from transactions order by id asc limit 5");
                    while ($row = mysqli_fetch_array($query)) {
                    ?>
                        <div>
                            <div>
                                <?php echo $row['id']; ?>
                                <br>
                                <?php echo $row['description']; ?>
                                <br>
                                <?php echo $row['promorefnum']; ?>
                                <br><br>
                            </div>
                        </div>
                    <?php
                        $lastid = $row['id'];
                    }
                    ?>
                    <div id="remove">
                        <div style="height:10px;"></div>
                        <div>
                            <div>
                                <div id="load-more" data-id="<?php echo $lastid; ?>">Load More</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script>
            $(document).ready(function() {
                $(document).on('click', '#load-more', function() {
                    var lastid = $(this).data('id');
                    $('#load-more').html('Loading...');
                    $.ajax({
                        url: "load-data.php",
                        method: "POST",
                        data: {
                            lastid: lastid,
                        },
                        success: function(data) {
                            if (data != '') {
                                $('#remove').remove();
                                $('#load-content').append(data);
                            } else {
                                $('#load-more').html('End Of Data');
                            }
                        }
                    });
                });
            });
        </script>
    </body>

    </html>

加载更多内容

   <!DOCTYPE html>
<html>

<head>
    <title>Load More Scroll</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>

<body>
    <div class="container">
        <div>
            <h2 align="center">Load More Scroll</h2>
            <div style="height:10px;"></div>
            <div id="load-content">
                <?php
                $lastid = '';
                include('connection.php');
                $query = mysqli_query($connection, "select * from transactions order by id asc limit 11");
                while ($row = mysqli_fetch_array($query)) {
                ?>
                    <div>
                        <div>
                            <?php echo $row['id']; ?>
                            <br>
                            <?php echo $row['description']; ?>
                            <br>
                            <?php echo $row['promorefnum']; ?>
                            <br><br>
                        </div>
                    </div>
                <?php
                    $lastid = $row['id'];
                }

                ?>

                <div id="remove">
                    <div style="height:10px;"></div>
                    <div>
                        <div>
                            <div id="load-more" data-id="<?php echo $lastid; ?>"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            window.onscroll = function() {
                if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2) {
                    var lastid = $('#load-more').data('id');
                    $('#load-more').html('Loading...');
                    $.ajax({
                        url: "load-data.php",
                        method: "POST",
                        data: {
                            lastid: lastid,
                        },
                        success: function(data) {
                            if (data != '') {
                                $('#remove').remove();
                                $('#load-content').append(data);
                            } else {
                                $('#load-more').html('End Of Data');
                            }
                        }
                    });
                }
            }
        });
    </script>
</body>

</html>


PHP代码/加载文件

<?php
sleep(1);
include('connection.php');
if (isset($_POST['lastid'])) {
     $lastid = $_POST['lastid'];
     $query = mysqli_query($connection, "select * from transactions where id > '$lastid' order by id asc limit 10");

     if (mysqli_num_rows($query) > 0) {
          while ($row = mysqli_fetch_array($query)) {
?>
               <div>
                    <?php echo $row['id']; ?>
                    <br>
                    <?php echo $row['description']; ?>
                    <br>
                    <?php echo $row['promorefnum']; ?>
                    <br><br>
               </div>
          <?php
               $lastid = $row['id'];
          }
          ?>
          <div id="remove">
               <div style="height:10px;"></div>
               <div>
                    <div>
                         <div id="load-more" data-id="<?php echo $lastid; ?>">Load More</div>
                    </div>
               </div>
          </div>
<?php
     }
}
?>

php jquery ajax scroll
1个回答
0
投票

尝试此检查

<head>
    <title>Load More Scroll</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>

<body>
    <div class="container">
        <div>
            <h2 align="center">Load More Scroll</h2>
            <div style="height:10px;"></div>
            <div id="load-content">
                <?php
                $lastid = '';
                include('connection.php');
                $query = mysqli_query($connection, "select * from transactions order by id asc limit 11");
                while ($row = mysqli_fetch_array($query)) {
                ?>
                    <div>
                        <div>
                            <?php echo $row['id']; ?>
                            <br>
                            <?php echo $row['description']; ?>
                            <br>
                            <?php echo $row['promorefnum']; ?>
                            <br><br>
                        </div>
                    </div>
                <?php
                    $lastid = $row['id'];
                }

                ?>

                <div id="remove">
                    <div style="height:10px;"></div>
                    <div>
                        <div>
                            <div id="load-more" data-id="<?php echo $lastid; ?>"></div>
                        </div>
                    </div>
                </div>
                <span id="loading">Loading...</span>
            </div>
        </div>
    </div>

    <script>
        $(document).ready(function() {
            $("#loading").hide();
            window.onscroll = function() {
                if ((window.innerHeight + window.pageYOffset) >= document.body.offsetHeight - 2) {
                    var lastid = $('#load-more').data('id');
                    $('#load-more').html('Loading...');
                    if($("#loading").css('display') == 'none') {
                        $("#loading").show();
                        $.ajax({
                            url: "load-data.php",
                            method: "POST",
                            data: {
                                lastid: lastid,
                            },
                            success: function(data) {
                                if (data != '') {
                                    $('#remove').remove();
                                    $('#load-content').append(data);
                                    $("#loading").hide();
                                } else {
                                    $('#load-more').html('End Of Data');
                                }
                            }
                        });
                    }
                }
            }
        });
    </script>
</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.