响应后附加在 HTML 中的未知文本

Posted

技术标签:

【中文标题】响应后附加在 HTML 中的未知文本【英文标题】:Uknown text appending in HTML after response 【发布时间】:2019-06-21 04:22:39 【问题描述】:

我一直在尝试实现无限滚动,在 Ajax 中成功调用之后,当我将结果附加到 html 时,它总是给我一个不必要的数字 1,如图所示,我不知道在哪里它来自

Pagination.php

<?php
include 'connection.php';

// header('Content-Type: application/json');

$pageno = $_POST['pageno'];

$no_of_records_per_page = 5;
$offset = ($pageno-1) * $no_of_records_per_page;

$sql = "SELECT *, 
  posts.id AS post_id, 
  posts.created_at AS post_created_at 
  FROM posts 
  LEFT JOIN users_account 
  ON posts.user_id = users_account.id 
  LEFT JOIN users_personal_info
  ON users_account.id = users_personal_info.user_id
  ORDER BY post_id DESC LIMIT $offset, $no_of_records_per_page";
$stmt = $connect->prepare($sql);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_OBJ);
$post_count = $stmt->rowCount();
if ($post_count > 0) 
    $results = include('data.php');
    echo $results;
 else 
    echo 'maxed';

Index.php

<?php


include 'connection.php'; 

?>
<!DOCTYPE html>
<html lang="en">
<head>
  <?php include 'mda_header.php' ?>
  <style>

  #loader
      display: block;
      margin: auto;
  
</style>
</head>
<body>

<div class="container">
    <?php
        $sql = "SELECT *, 
          posts.id AS post_id, 
          posts.created_at AS post_created_at 
          FROM posts 
          LEFT JOIN users_account 
          ON posts.user_id = users_account.id 
          LEFT JOIN users_personal_info
          ON users_account.id = users_personal_info.user_id
          ORDER BY post_id DESC LIMIT 5";
        $stmt = $connect->prepare($sql);
        $stmt->execute(); 
        $rows = $stmt->fetchAll(PDO::FETCH_OBJ);
        ?>
        <?php include 'data.php' ?>
  <div id="response"></div>
</div>
  <input type="hidden" id="pageno" value="1">
  <img id="loader" src="https://i.redd.it/ounq1mw5kdxy.gif">
  <script src="jquery.inview.min.js"></script>
  <script>
    $(document).ready(function()
      $('#loader').on('inview', function(event, isInView) 
          if (isInView) 
              var nextPage = parseInt($('#pageno').val()) + 1;
              console.log(nextPage);
              var total = parseInt($('#total').val());
              $.ajax(
                  type: 'POST',
                  url: 'pagination.php',
                  data:  pageno: nextPage, total: total ,
                  success: function(response)
                    if (response != 'maxed') 
                      $('#response').append(response);
                      $('#pageno').val(nextPage);
                     else 
                      $('#loader').hide();
                    
                  
              );
          
      );
  
  </script>
</body>
</html>

这是我获取结果的地方,我在代码中没有看到任何数字

data.php

<?php foreach ($rows as $row): ?>
      <div class="media">
        <img class="align-self-start mr-3 rounded-circle" src="uploads/<?= $row->image_upload ?>"   >
        <div class="media-body">
          <h5 class="mt-0">
            <a href="mdaProfileView.php?user=<?= $row->username ?>"
            data-post-author="<?= $row->username ?>" 
            data-placement="right" 
            data-container="body" 
            data-toggle="popover" 
            data-trigger="hover"
            data-html="true" 
            title="<b><?= ucfirst($row->fname) ?> <?= ucfirst($row->lname) ?>, <?= $row->age ?></b>" 
            data-content="<p class='text-muted'><?= empty($row->description) ? 'No description' : $row->description ?></p>"
            >

              <?= $row->username ?>
            </a>

            </h5>
          <small class="text-muted">
            <i>
              <i class="fa fa-clock-o"></i> 
              <span class="time" data-time-post-id="<?= $row->post_id ?>"><?= time_elapsed($row->post_created_at) ?></span>
            </i>
          </small>

          <p><small class="text-muted post-body"><?= $row->body ?></small></p>
           <?php  
           $like_stmt = $connect->prepare("SELECT *, COUNT(*) AS total_likes FROM likes WHERE post_id = $row->post_id");
           $like_stmt->execute();
           $like_row = $like_stmt->fetch(PDO::FETCH_OBJ);

           $comment_stmt = $connect->prepare("SELECT *, COUNT(*) AS total_comments FROM comments WHERE post_id = $row->post_id");
           $comment_stmt->execute();
           $comment_row = $comment_stmt->fetch(PDO::FETCH_OBJ);
           ?>
          <ul class="list-inline">
           <li class="list-inline-item">
            <small class="text-muted">
              <span class="likes_total_<?= $row->post_id ?>">
                <?= $like_row->total_likes ?></span><?= $like_row->total_likes > 1 ? ' Likes' : ' Like' ?>
            </small>
           </li>
           <li class="list-inline-item">
            <small class="text-muted">
              <span class="comments_total_<?= $row->post_id ?>">
                <?= $comment_row->total_comments ?>
              </span>
              <?= $comment_row->total_comments > 1 ? ' Comments' : ' Comment' ?>
            </small>
           </li>
           <li class="list-inline-item float-right">
            <small class="text-muted">25 Shares</small>
           </li>
         </ul>
           <ul class="list-inline">
            <li class="list-inline-item">
             <small>
                <?php  
                $user_id = user()->user_original_id;
                $isUserLiked_stmt = $connect->prepare("SELECT * FROM likes WHERE post_id = $row->post_id AND user_id = $user_id");
                $isUserLiked_stmt->execute();
                ?>
                <a 
                class="social-icon text-xs-center like like_button_<?= $row->post_id ?><?= $isUserLiked_stmt->rowCount() ? ' bold' : '' ?>" 
                data-user-id="<?= user()->user_original_id ?>" 
                data-author="<?= $row->username ?>" 
                data-post-id="<?= $row->post_id ?>" 
                data-type="post"
                href="javascript:void(0)">
                <i class="fa fa-thumbs-up up"
                ></i> Like</a>
              </small>
           </li>
            <li class="list-inline-item">
              <!-- comment_button -->
             <small><a class="social-icon text-xs-center comment" 
              href="javascript:void(0)" 
              data-post-id="<?= $row->post_id ?>" 
              data-toggle="modal" 
              data-target="#exampleModal"><i class="fa fa-comment"></i> Comment</a></small>
           </li>
            <li class="list-inline-item float-right">
             <small><a class="social-icon text-xs-center" href="javascript:void(0)"><i class="fa fa-share"></i> Share</a></small>
           </li>
          </ul>
        </div>
      </div>
   <?php endforeach; ?> 

【问题讨论】:

如果将&lt;input type="hidden" id="pageno" value="1"&gt; 替换为&lt;input type="hidden" id="pageno" value="2"&gt; 会发生什么情况现在数字1 变成2了吗? 【参考方案1】:

您只需要一个包含 - 您也不需要回显结果,您可能会发现 $results1 表示 include() 成功。改变

$results = include('data.php');
echo $results;

include('data.php');

如果包含的脚本使用return 传回数据,则只需要赋值和echo

【讨论】:

以上是关于响应后附加在 HTML 中的未知文本的主要内容,如果未能解决你的问题,请参考以下文章

使用 JQuery ajax 在 DOM 操作后附加事件

在文件中的某个单词之后附加文本

单击表单中的按钮后,通过 Ajax 附加 html 页面

附加题

如何使用 PHP 将文本附加到特定标签内的外部 html 文件?

D3 将文本附加到 SVG 矩形