您只需要一个带有 SQL 查询的服务器端脚本,该脚本将返回更新的帖子。
让您的 javascript 存储一个日期变量或最后一个帖子 id 的变量(使用 PHP 进行澄清):
result = mysql_query("SELECT ID,POST FROM POSTS WHERE DATE>" . $_GET['date']); //or use WHERE ID> $_GET['id']
while(rows[] = mysq_fetch_array(query));
print json_encode(rows);
现在你有一个服务器端脚本会返回新的帖子,所以你所要做的就是为更多按钮编写 javascript 函数:
updatePosts = function () {
$.ajax({
url: 'serversiderUrl?lastId=' + last_id, //last_id is global variable for the id of the last post on the page
success: function(data){
data = JSON.parse(data);
for(i in data){
$('#posts_container').append(data[i].post); //do your appending functions here
last_id = data[i].id;
}
}
}
现在为发布新条目创建一个您喜欢的语言的服务器端脚本来处理新帖子:
result = mysql_query("INSERT INTO POSTS VALUES(''," . urldecode($_POST['POST']) . ")");
现在是客户端:
submit_post = function(){
$.ajax({
type: 'POST',
url:'yourposturl',
data: "post=" + encodeURIComponent($('#textArea').text()),
success: function(){
updatePosts(); // call the function that update the posts so the new entry is now added to the page
}
});
}
现在在文档完全加载时将函数绑定到相应的按钮:
$(document).ready(function (){
$('#moreButtonId').click(updatePosts);
$('#submitButtonId').click(submitPost);
});