du59131 2017-03-10 03:36
浏览 103

在帖子评论部分显示评论而不重新加载页面

im trying to make a facebook style post and comment section but i dont know how to display the data inserted to my database without refreshing the page...

this code is for saving the data to my db. i use window.location.reload(); to reload my page so that the data will be displayed on my page..

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

using this script i can display my comment on a post i need to refresh the page first for me to be able to show the comment.

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

what im trying to do is that this is where i want to display my comments from my db. i tried researching about append/load but i dont exactly know how this works. is there any idea that i can display my comment in this script?

<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>
  • 写回答

1条回答 默认 最新

  • dpzjl68484 2017-03-10 03:43
    关注

    I decided to write some pseudo code for you here. If you don't know how to store and fetch comments I would recommend looking into MYSQL. It's relatively simple (for simple things), so that shouldn't be too big of a problem. YouTube tutorials will be your blessing there.

    You should have at least three files to properly implement this:

    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>
    

    Note: Although it may be annoying, if you want immediate satisfaction, append the new comment to the end and then invoke refreshComments. (But I wouldn't recommend doing this because it would force you to update multiple locations in your code whenever you change your comment HTML format).

    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序