Ajax应用:jQuery+PHP+MySQL发表评论

Ajax应用:jQuery+PHP+MySQL发表评论

Ajax应用:jQuery+PHP+MySQL发表评论

jQuery+PHP+MySQL发表评论留言板

源码介绍

HTML

首先我们放置一个评论表单和显示评论列表#comments

 id="post">  
   >发表评论  
   >昵称:

  
   > type="text" class="input" id="user" />

  
   >评论内容:

  
   > class="input" id="txt" style="width:100%; height:80px">

               

jQuery

接着调用评论列表,并且通过Ajax发布评论:

$(function() { 
    var comments = $("#comments"); 
    $.getJSON("ajax.php"
    function(json) { 
        $.each(json, 
        function(index, array) { 
            var txt = "

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

"

            comments.append(txt); 
        }); 
    }); 
 
    $("#add").click(function() { 
        var user = $("#user").val(); 
        var txt = $("#txt").val(); 
        $.ajax({ 
            type: "POST"
            url: "comment.php"
            data: "user=" + user + "&txt=" + txt, 
            success: function(msg) { 
                if (msg == 1{ 
                    var str = "

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

"

                    comments.append(str); 
                    $("#message").show().html("发表成功!").fadeOut(1000); 
                    $("#txt").attr("value"""); 
                } else { 
                    $("#message").show().html(msg).fadeOut(1000); 
                } 
            } 
        }); 
    }); 
});

               

Mysql

最后附上表comments结构:

CREATE TABLE `comments` (  
  `id` int(11) NOT NULL auto_increment,  
  `user` varchar(30) NOT NULL,  
  `comment` varchar(200) NOT NULL,  
  `addtime` datetime NOT NULL,  
  PRIMARY KEY  (`id`)  
) ENGINE=MyISAM;


分享到 :

发表评论

登录... 后才能评论