我试图制作一个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
}
}
?>
我想要做的是,这是我想从我的数据库显示我的评论的地方。我尝试研究有关追加/加载,但我不知道这是如何工作的。有什么想法我可以在这个剧本中显示我的评论吗?
<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>
答案 0 :(得分:1)
我决定在这里为你写一些伪代码。如果您不知道如何存储和获取注释,我建议您查看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>
注意:尽管可能很烦人,但如果您想立即满意,请将新注释附加到结尾,然后调用refreshComments。 (但我不建议这样做,因为每当您更改注释HTML格式时,它都会强制您更新代码中的多个位置)。