我正在尝试制作 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>
我决定在这里为您编写一些伪代码。如果您不知道如何存储和获取评论,我建议您研究一下 MYSQL。它相对简单(对于简单的事情),所以这应该不是什么大问题。 YouTube 教程将是你的福音。
您应该至少有三个文件才能正确实现此功能:
上传评论.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 格式时,它都会迫使您更新代码中的多个位置)。
我们了解马来西亚学生面临的独特挑战,这就是我们在这里提供帮助的原因。我们的作业帮助服务专为满足马来西亚学生的特定需求而量身定制,为他们提供可靠且有效的解决方案,以取得优异的学业成绩。
感谢您阅读本文! 😊