如何在不刷新浏览器的情况下加载更多评论

时间:2018-06-24 01:24:57

标签: php jquery

我正在使用注释系统,但被卡在这里,在将PHP加载到浏览器后,它向我显示了我想看到的2条注释,当我单击链接时,它将在编写程序时获取其他2条注释jQuery,但此后该按钮消失,我无法加载更多评论。

请帮助!

这是我的代码,

 <script>
 $(document).ready(function() {
 var commentCount = 2;
 $("button").click(function() {
 commentCount = commentCount + 2;
 $("#comments").load("2.php", {
 commentNewCount: commentCount

 });
 });

 });

</script>
<body>
<div id="comments">
<?php 
$sql = "SELECT * FROM comments ORDER BY id LIMIT 2";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><a href="#"><?php echo $row['author']; ?></a></h5>
</div>
<div class="media-body response-text-right">
 <p><?php  echo $row['message']; ?></p>
 <ul>
 <li><?php echo $row['time']; ?> </li>
 <li><a href="single.html">Reply</a></li>
 </ul>
 </div>
 </div>

<?php   }

} else {
    echo "there are no comments!";
}
?>
<button>More comments</button>

</body>

这是我的load-coments.php(我决定将其命名为2.php)

<?php 
include 'include/dbconnect.php';
$commentNewCount = $_POST['commentNewCount'];
    $sql = "SELECT * FROM comments ORDER BY id LIMIT $commentNewCount";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<div class="media response-info">
<div class="media-left response-text-left">
<a href="#">
<img class="media-object" src="images/c1.jpg" alt="">
</a>
<h5><a href="#"><?php echo $row['author']; ?></a></h5>
</div>
<div class="media-body response-text-right">
<p><?php  echo $row['message']; ?></p>
<ul>
<li><?php echo $row['time']; ?> </li>
<li><a href="single.html">Reply</a></li>
</ul>
</div>
</div>

<?php   }

} else {
    echo "there are no comments!";
}
?>

1 个答案:

答案 0 :(得分:1)

“按钮消失”是什么意思? jQuery的load函数用接收到的html替换html,因此您的按钮可能在要替换的div中。由于缺少缩进,因此很难调试代码。