Chinaunix首页 | 论坛 | 博客
  • 博客访问: 394431
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 125
  • 用 户 组: 普通用户
  • 注册时间: 2018-07-07 22:36
文章分类
文章存档

2020年(9)

2019年(14)

2018年(2)

我的朋友

分类: PHP

2020-03-04 09:28:10

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



首先我们放置一个评论表单和显示评论列表#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;

本文转自: 转载请注明出处!
阅读(2023) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~