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

首先我们放置一个评论表单和显示评论列表#comments,接着调用评论列表,并且通过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);
}
}
});
});
});
最后附上表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;
本文转自:
https://www.sucaihuo.com/php/84.html 转载请注明出处!
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31545264/viewspace-2678178/ ,如需转载,请注明出处,否则将追究法律责任。