donglieshe4692 2018-09-07 09:35
浏览 55

通过PHP注入HTML和jQuery出错了

I am beginner and I was trying to dynamically load stuff from database which was successful. Then my next goal was to implement infinite scroll with javascript, but I think the HTML code is being injected into wrong places or something, because when I use data-columns and try to load more with scroll, the styling goes crazy. If I take that out, it loads fine, but the style of my page is all over the place.

Screenshot how is should look like

How it looks when I load 4 items at the start and then additional 4 when reaching bottom page

Index.php html in which the data is injected

<div id="fh5co-main">
    <div class="container">

        <div class="row">

            <div id="fh5co-board" class="results" data-columns>


            </div>
        </div>
    </div>
</div>

Javascript for infinite load in index.php

<script type="text/javascript">


    var start = 0;
    var limit = 4;
    var reachedMax = false;

    $(window).scroll(function () {
        if($(window).scrollTop() == $(document).height() - $(window).height())
            getData();

    });

    $(document).ready(function () {
        getData();

    });

    function getData() {
        if(reachedMax)
            return;

        $.ajax({
            url: 'load_more.php',
            method: 'POST',
            dataType: 'text',
            data: {
                getData: 1,
                start: start,
                limit: limit
            },
            success: function (response) {
                if (response == "reachedMax")
                    reachedMax = true;
                else {
                    start += limit;
                    $(".results").append(response);


                }

            }
        });

    }



</script>

load_more.php

<?php include_once "includes/functions.php"?>

<?php

if(isset($_POST['getData'])) {

    $start = $_POST['start'];
    $limit = $_POST['limit'];

    $query = "SELECT title, image, text FROM posts LIMIT $start, $limit";
    $result = mysqli_query(DatabaseConnect(), $query);

    if (mysqli_num_rows($result) > 0) {
        $response = "";

        while ($data = mysqli_fetch_array($result)) {

            $response .= '
            <div class="item">
        <div class="animate-box">
            <a href="images/' . $data['image'] . '" class="image-popup fh5co-board-img" title="' . $data['title'] . '"><img src="images/' . $data['image'] . '" alt="image"></a>
        </div>
        <h3>' . $data['title'] . '</h3>
        <div class="fh5co-desc">' . $data['text'] . '</div>
    </div>';
        }

        exit($response);

    } else
        exit('reachedMax');


}

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog