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


    分享一个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 = "<p><strong>" + array["user"] + "</strong>:" + array["comment"] + "<span>" + array["addtime"] + "</span></p>"; 
     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 = "<p><strong>" + user + "</strong>:" + txt + "<span>刚刚</span></p>"; 
    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 转载请注明出处!

  • 相关阅读:
    喜欢的事,用心去做!
    初识mysql
    使用连发互联空间+SQLyog 设置我们的数据库链接
    checkbox实现全选全不选
    各种电脑进入BIOS快捷键
    Fetching data with Ajax小例子
    JS 变量类型互相转换
    XMLHTTPRequestObject获取服务器数据
    js事件
    关闭华硕笔记本触摸屏
  • 原文地址:https://www.cnblogs.com/zglevk/p/12393891.html
Copyright © 2020-2023  润新知