分享一个PHP+MySQL+Ajax设计的高效发表评论留言功能,可以将此功能应用在网站留言、评论等地方。

PHP+MySQL设计高效发表评论留言功能-LMLPHP

首先我们放置一个评论表单和显示评论列表#comments,接着调用评论列表,并且通过Ajax发布评论:
  1. $(function() {
  2.     var comments = $("#comments");
  3.     $.getJSON("ajax.php",
  4.     function(json) {
  5.         $.each(json,
  6.         function(index, array) {
  7.             var txt = "

    " + array["user"] + ":" + array["comment"] + "" + array["addtime"] + "

    "
    ;
  8.             comments.append(txt);
  9.         });
  10.     });
  11.  
  12.     $("#add").click(function() {
  13.         var user = $("#user").val();
  14.         var txt = $("#txt").val();
  15.         $.ajax({
  16.             type: "POST",
  17.             url: "comment.php",
  18.             data: "user=" + user + "&txt=" + txt,
  19.             success: function(msg) {
  20.                 if (msg == 1) {
  21.                     var str = "

    " + user + ":" + txt + "刚刚

    "
    ;
  22.                     comments.append(str);
  23.                     $("#message").show().html("发表成功!").fadeOut(1000);
  24.                     $("#txt").attr("value", "");
  25.                 } else {
  26.                     $("#message").show().html(msg).fadeOut(1000);
  27.                 }
  28.             }
  29.         });
  30.     });
  31. });

最后附上表comments结构:
  1. CREATE TABLE `comments` (
  2.   `id` int(11) NOT NULL auto_increment,
  3.   `user` varchar(30) NOT NULL,
  4.   `comment` varchar(200) NOT NULL,
  5.   `addtime` datetime NOT NULL,
  6.   PRIMARY KEY (`id`)
  7. ) ENGINE=MyISAM;

本文转自:https://www.sucaihuo.com/php/84.html 转载请注明出处!
12-17 11:53