【问题标题】:PHP MYSQLI AJAX Load On Scroll Not WorkingPHP MYSQLI AJAX 滚动加载不工作
【发布时间】:2020-04-23 02:18:56
【问题描述】:

我有在按钮点击时加载更多内容的代码。我还有另一个用于加载滚动。按钮的代码加载正常。滚动代码将新加载的内容复制了 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
     }
}
?>

【问题讨论】:

  • LOAD ON CLICK WITH BUTTON 和 LOAD MORE ON SCROLL 有相同的代码吗?
  • 哎呀。复制粘贴失败。解决了这个问题。
  • 我很感激。这是对页面上显示的没有输入功能的信息的担忧吗?您可以单击它们进行操作,仅此而已。我已经将准备好的语句用于具有任何类型输入的所有内容。我还清理了输入。我没有将其转换为不允许用户输入的任何内容。

标签: php jquery ajax scroll


【解决方案1】:

试试这个检查

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 2016-04-18
    相关资源
    最近更新 更多