头像

PHP+jQuery+MySQL发表评论

2017-07-14 23:48:28 收藏    来源:PHP代码   浏览()   评论 ( 0 )   

PHP+jQuery+MySQL发表评论
在本文中,我将带你一起使用PHP,MySQL和jQuery创建一个快速高效的发表评论的功能。您可以将此功能应用在留言、评论等领域。
查看演示 下载资源:0

手机扫码访问:

下载资源 下载积分:50

  HTML

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

   <h3>发表评论</h3>  
   <p>昵称:</p>  
   <p><input type="text" class="input" id="user" /></p>  
   <p>评论内容:</p>  
   <p><textarea 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 = "<p><strong>" + array["user"] + "</strong>:" + array["comment"] + "<span>" + array["addtime"] + "</span></p>"; 
                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 = "<p><strong>" + user + "</strong>:" + txt + "<span>刚刚</span></p>"; 
                        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  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


标签: 评论留言
声明:转载请注明来源(PHP代码)并保留原文链接:http://www.phpdaima.com/upload-21.html
广告不存在
评论0

后面还有条评论,点击查看>>

温馨提示:为规范评论内容,垃圾评论一律封号...

PHP+jQuery+MySQL发表评论
我的积分余额: 0.0 已下载次数: 0
所需积分:5050 开始下载
×