首页>>表单>>jQuery+PHP+MySQL发表评论,无刷新评论完整代码(2016-04-25)

jQuery+PHP+MySQL发表评论,无刷新评论完整代码

jQuery+PHP+MySQL发表评论,无刷新评论完整代码
赞赏支持
立刻微信赞赏支持 关闭

 

JavaScript Code
  1. <script type="text/javascript">  
  2.             $(function() {  
  3.                 var comments = $("#comments");  
  4.                 $.getJSON("ajax.php"function(json) {  
  5.                     $.each(json, function(index, array) {  
  6.                         var txt = "<p><strong>" + array["user"] + "</strong>:" + array["comment"] + "<span>" + array["addtime"] + "</span></p>";  
  7.                         comments.append(txt);  
  8.                     });  
  9.                 });  
  10.   
  11.                 $("#add").click(function() {  
  12.                     var user = $("#user").val();  
  13.                     var txt = $("#txt").val();  
  14.                     $.ajax({  
  15.                         type: "POST",  
  16.                         url: "comment.php",  
  17.                         data: "user=" + user + "&txt=" + txt,  
  18.                         success: function(msg) {  
  19.                             if (msg == 1) {  
  20.                                 var str = "<p><strong>" + user + "</strong>:" + txt + "<span>刚刚</span></p>";  
  21.                                 comments.prepend(str);  
  22.                                 $("#message").show().html("发表成功!").fadeOut(1000);  
  23.                                 $("#txt").attr("value""");  
  24.                             } else {  
  25.                                 $("#message").show().html(msg).fadeOut(1000);  
  26.                             }  
  27.                         }  
  28.                     });  
  29.                 });  
  30.             });  
  31.         </script>  
XML/HTML Code
  1. <div class="demo">  
  2.                 <div id="post">  
  3.                     <h3>发表评论</h3>  
  4.                     <p>昵称:</p>  
  5.                     <p><input type="text" class="input" id="user" /></p>  
  6.                     <p>评论内容:</p>  
  7.                     <p><textarea class="input" id="txt" style="width:100%; height:80px"></textarea></p>  
  8.                     <p><input type="submit" class='btn'value="发表" id="add" /></p>  
  9.                     <div id="message"></div>  
  10.                 </div>  
  11.                 <div id="comments"></div>  
  12.             </div>  

ajax.php

 

PHP Code
  1. require('conn.php');  
  2.   
  3. $q=mysql_query("select * from comments order by id desc");  
  4. while($row=mysql_fetch_array($q)){  
  5.         $comments[] = array("id"=>$row['id'],"user"=>$row['name'],"comment"=>$row['body'],"addtime"=>$row['dt']);  
  6. }  
  7. echo json_encode($comments);  

comment.php

 

PHP Code
  1. $user = htmlspecialchars(trim($_POST['user']));  
  2. $txt = htmlspecialchars(trim($_POST['txt']));  
  3. if(emptyempty($user)){  
  4.    echo "昵称不能为空";  
  5.    exit;  
  6. }  
  7. if(emptyempty($txt)){  
  8.    echo "评论内容不能为空";  
  9.    exit;  
  10. }  
  11. $time = date("Y-m-d H:i:s");  
  12. $query=mysql_query("insert into comments(name,body,dt)values('$user','$txt','$time')");  
  13. if($query)  echo "1";  

 


原文地址:http://www.freejs.net/article_biaodan_552.html