在未重新加载页面的情况下在评论部分中显示注释



我试图制作Facebook样式帖子和评论部分,但我不知道如何在不刷新页面的情况下显示插入数据库的数据...

此代码用于将数据保存到我的数据库中。我使用window.location.reload();重新加载我的页面,以便将数据显示在我的页面上。

<script>
$(document).ready(function() {
$('input[name="mycomment"]').on('keyup', function(e){
e.preventDefault();
var comments = $(this).val();
var sid = $(this).closest("div#userspost").find("input[type='hidden']").val();
if(e.keyCode == 13){        
if(comments.length)
$.ajax({
url: "../controller/post_controller.php",
type: "POST",
data:{ 
"id":sid,
"comments":comments,
},
success: function(data)
{
window.location.reload();
}
});
else
alert("Please write something in comment.");
}
});
}); 
</script>

使用此脚本我可以在帖子上显示我的评论,我需要先刷新页面才能显示评论。

<?php 
foreach ($post_model->getcomment() as $value) {
if($postid == $value['post_uid']){
?>
<div id="mycomments">
<div class="col-lg-12" style="background:#eff9c7;">
<img src="./<?php echo $value['image']?>" class="pull-left" style="border-radius:50%;margin-top:10px;" width="7%" height="7%" />
<p style="margin-top:18px;line-height:15px;"><strong class="font-1" style="margin-left:10px;"><?php echo $value['firstname'].' '.$value['lastname']?></strong> <?php echo $value['pc_comment']?><br>
<span class="" style="margin-left:10px;font-size:.9em;color:gray;"><abbr class="timeago" title="<?php echo $value['pc_datesend']?>"></abbr></span> 
</p>
</div>
</div>
<?php
}
}
?>

我想做的是这是我要从DB中显示我的评论的地方。我尝试研究有关附加/负载的研究,但我不完全知道这是如何工作的。有什么想法可以在此脚本中显示我的评论吗?

<div id="mycomments">
<div class="col-lg-12" style="background:#eff9c7;">
<img src="./<?php echo $value['image']?>" class="pull-left" style="border-radius:50%;margin-top:10px;" width="7%" height="7%" />
<p style="margin-top:18px;line-height:15px;"><strong class="font-1" style="margin-left:10px;"><?php echo $value['firstname'].' '.$value['lastname']?></strong> <?php echo $value['pc_comment']?><br>
<span class="" style="margin-left:10px;font-size:.9em;color:gray;"><abbr class="timeago" title="<?php echo $value['pc_datesend']?>"></abbr></span> 
</p>
</div>
</div>

我决定在这里为您编写一些伪代码。如果您不知道如何存储和获取评论,我建议您查看MySQL。这是相对简单的(对于简单的事情),因此这不应该太大问题。YouTube教程将是您在那里的祝福。

您应该至少有三个文件来正确实现:

uploadcomment.php

<?php
//process the comment upload
echo $_POST['comment'];
?>

getComment.php

<?php
//however you serve commnets, MYSQL, maybe?
//make sure it's properly formatted with HTML
?>

index.html

<form>
    <input type="text" name="comment" id="comment" value="Comment here" />
    <button id="submit">Submit</button>
</form>
<div id="comments">
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
$("#submit").click(function () {
    $.post("uploadComment.php", {
        comment : $("#comment").val()
    }, function (data) {
        //comment posted.
        refreshComments();
    });
});
function refreshComments() {
    $.get("getComments.php", function(data) {
        $("#comments").html(data);
    });
}
setInterval(refreshComments,5000);

</script>

注意:尽管这可能很烦人,如果您想立即满意,请将新评论附加到最后,然后调用刷新收获。(但是我不建议这样做,因为它会迫使您在更改评论HTML格式时更新代码中的多个位置)。

相关内容

最新更新