微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

thinkPHP实现基于ajax的评论回复功能

本文实例讲述了thinkPHP实现基于ajax的评论回复功能分享给大家供大家参考,具体如下:

控制器代码:

rush:PHP;"> count(); //获取评论总数 $this->assign('num',$num); $data=array(); $data=$this->getCommlist();//获取评论列表 $this->assign("commlist",$data); $this->display('index'); } /** *添加评论 */ public function addComment(){ $data=array(); if((isset($_POST["comment"]))&&(!empty($_POST["comment"]))){ $cm = json_decode($_POST["comment"],true);//通过第二个参数true,将json字符串转化为键值对数组 $cm['create_time']=date('Y-m-d H:i:s',time()); $newcm = M('comment'); $id = $newcm->add($cm); $cm["id"] = $id; $data = $cm; $num = M('comment')->count();//统计评论总数 $data['num']= $num; }else{ $data["error"] = "0"; } echo json_encode($data); } /** *递归获取评论列表 */ protected function getCommlist($parent_id = 0,&$result = array()){ $arr = M('comment')->where("parent_id = '".$parent_id."'")->order("create_time desc")->select(); if(empty($arr)){ return array(); } foreach ($arr as $cm) { $thisArr=&$result[]; $cm["children"] = $this->getCommlist($cm["id"],$thisArr); $thisArr = $cm; } return $result; } }

JavaScript代码:

rush:js;"> $(function(){ //点击提交评论内容 $('body').delegate('.comment-submit','click',function(){ var content = $.trim($(this).parent().prev().children("textarea").val());//根据布局结构获取当前评论内容 $(this).parent().prev().children("textarea").val("");//获取内容后清空输入框 if(""==content){ alert("评论内容不能为空!"); }else{ var cmdata = new Object(); cmdata.parent_id = $(this).attr("parent_id");//上级评论id cmdata.content = content; cmdata.nickname = "游客";//测试用数据 cmdata.head_pic = "/Public/images/default.jpg";//测试用数据 var replyswitch = $(this).attr("replyswitch");//获取回复开关锁属性 $.ajax({ type:"POST",url:"/index.PHP/home/index/addComment",data:{ comment:JSON.stringify(cmdata) },dataType:"json",success:function(data){ if(typeof(data.error)=="undefined"){ $(".comment-reply").next().remove();//删除已存在的所有回复div //更新评论总数 $(".comment-num").children("span").html(data.num+"条评论"); //显示新增评论 var newli = ""; if(cmdata.parent_id == "0"){ //发表的是一级评论时,添加到一级ul列表中 newli = "
  • "+data.nickname+""+data.create_time+"

    "+data.content+"

    • "; $(".comment-ul").prepend(newli); }else{ //否则添加到对应的孩子ul列表中 if('off'==replyswitch){//检验出回复关闭锁存在,即三级评论不再提供回复功能 newli = "
    • "+data.nickname+""+data.create_time+"

      "+data.content+"

      • "; }else{//二级评论回复按钮要添加回复关闭属性 newli = "
      • "+data.nickname+""+data.create_time+"

        "+data.content+"

        • "; } $("li[comment_id='"+data.parent_id+"']").children("ul").prepend(newli); } }else{ //有错误信息 alert(data.error); } } }); } }); //点击"回复"按钮显示或隐藏回复输入框 $("body").delegate(".comment-reply","click",function(){ if($(this).next().length>0){//判断出回复div已经存在,去除掉 $(this).next().remove(); }else{//添加回复div $(".comment-reply").next().remove();//删除已存在的所有回复div //添加当前回复div var parent_id = $(this).attr("comment_id");//要回复评论id var divhtml = ""; if('off'==$(this).attr("replyswitch")){//二级评论回复后三级评论不再提供回复功能,将关闭属性附加到"提交回复"按钮" divhtml = ""; }else{ divhtml = ""; } $(this).after(divhtml); } }); })

          页面样式代码:

          rush:css;"> .comment-filed{ width:640px; margin:0 auto; } .comment-num{ text-align: right; font-size:14px; } .div-txt-submit{ text-align:right; margin-top:8px; } .comment-submit{ background-color:#63B8FF; margin-top:15px; text-decoration:none; color:#fff; padding:5px; font-size:14px; } .txt-commit{ border:1px solid blue; width:620px; height: 60px; padding: 10px; } .txt-reply{ width: 100%; height: 60px; } .comment-filed-list{ margin-top:20px; } .comment-list{ margin-top:2px; width:herit; height:50px; border-top:1px solid gray; } .comment-ul{ list-style:none; padding-left:0; } .head-pic{ width:40px; height:40px; } .cm{ position:relative; top:0px; left:40px; top:-40px; width:600px; } .cm-header{ padding-left:5px; } .cm-content{ padding-left:5px; } .cm-footer{ padding-bottom:15px; text-align:right; border-bottom: 1px dotted #CCC; } .comment-reply{ text-decoration:none; color:gray; font-size: 14px; } .children{ list-style:none; background-color:#FAFAFA; padding-left:0; margin-left:40px; } .children-cm{ position:relative; left:40px; top:-40px; width:90%; }

          页面布局代码:

          rush:xhtml;"> <Meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <a href="/tag/PHP/" target="_blank" class="keywords">PHP</a><a href="/tag/pinglun/" target="_blank" class="keywords">评论</a>及<a href="/tag/huifu/" target="_blank" class="keywords">回复</a><a href="/tag/gongneng/" target="_blank" class="keywords">功能</a>
          {$num}条评论