doubeizhong5178 2017-03-13 05:11
浏览 28

异步注释而不刷新页面

im trying to make a comment system what im trying to do is to send a comment to a post and will pop up right away after sending but i dont know exactly how to do it.. what i did was when i send a comment i use window.location.reload to pop up my comment

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

with this code i can show my comment and with sending the comment i use this.

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

    });
});

this is where i send my request.

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

what i want to do is when i send a comment it will show right after i send the comment and it will show like this enter image description here

i tried using $("#mycomments").append(data); but what happen is it only display the query of my db to insert the comment.. but not the html format.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测