【问题标题】:display comment in post comment section without reloading the page在评论部分显示评论而不重新加载页面
【发布时间】:2017-07-31 06:55:07
【问题描述】:

我正在尝试制作一个 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
}
}
?>

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

<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>

【问题讨论】:

  • 你在发帖前有没有故意“扁平化”代码?还是你写这样的代码?
  • 我将代码扁平化..
  • 使您的代码阅读能力下降

标签: javascript php jquery comments


【解决方案1】:

我决定在这里为你写一些伪代码。如果您不知道如何存储和获取 cmets,我建议您查看 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 格式时更新代码中的多个位置)。

【讨论】:

  • 我实际上可以存储我的数据库中的数据,如您所见,我还发布了我的循环,以便我能够显示存储的数据,但我需要先刷新页面,以便显示存储的评论向上。我想要做的是不重新加载页面我可以显示数据...
  • 这就是 $.get 的作用。它将获取您的后端网页以显示 cmets,然后将它们显示到页面上。
  • 我收到一个错误 .. 函数 refreshComments(function () { $.get("getComments.php", function(data) { $("#cmets").html(data ); }); });
  • 如果我的问题将得到解决但我还没有解决它,我会将其标记为解决方案。如果这对我有帮助,我无需担心,我会标记它..
  • 听起来不错,如果您需要帮助,我可以在 discorddiscord.gg/SjXTu3q
猜你喜欢
  • 2014-03-12
  • 2021-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 2020-01-14
  • 2020-07-28
  • 2014-10-30
相关资源
最近更新 更多