异步注释而不刷新页面

时间:2017-03-13 05:11:18

标签: php jquery html mysql ajax

我试图建立一个评论系统,我试图做的是发送评论到一个帖子,并会在发送后立即弹出,但我不知道该怎么做...我做的是当我发送一个评论我使用window.location.reload弹出我的评论

<div id="mycomments">
<?php 
  foreach ($post_model->getcomment() as $value) {
  if($postid == $value['post_uid']){

?>

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


<?php
}
}
?>
</div>

使用此代码我可以显示我的评论 并发送评论我使用此。

$(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.");
        }

    });
});

这是我发送请求的地方。

post_controller.php

<?php
if(session_id() == '' || !isset($_SESSION))
session_start();
$session_id = $_SESSION['id'];


  require_once('../model/dbconfig.php');
  require_once('../model/post.php');
  $database = new Database();
  $conn = $database->getConnection();
  $com = new post($conn);
    $comments = ($_POST['comments']);
  $uid = ($_POST['id']);
 if(isset($_POST['id']) && isset($_POST['comments'])){

    $res = $com->comment($uid,$session_id,$comments);
  }
?>  

我想要做的是当我发送评论时,它会在发送评论后立即显示,它会显示为enter image description here

我尝试使用$("#mycomments").append(data);但发生的情况是它只显示我的数据库的查询以插入注释..但不是html格式。

0 个答案:

没有答案
相关问题