分享一個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 轉載請注明出處!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/80854.html
標籤:PHP
