如何从用户提交的帖子中制作提要 [英] How to Make a Feed From User Submitted Posts
问题描述
我正在尝试弄清楚如何使用AJAX创建类似Twitter的feed,在用户按下提交"按钮后立即在同一页面上显示用户的帖子.这将是一个无限馈送站点,其底部将具有一个更多"按钮.
I'm trying to figure out how to use AJAX to create a Twitter-like feed that displays user's posts on the same page immediately after they push the submit button. It would be an infinite-feed site that would have a "more" button at the bottom.
我要做的只是一个简单的页面,其中包含一个带有提交"按钮的textarea框,并在提交时在框下方显示用户提交的内容.
All I'm trying to make is a simple page containing a textarea box with a submit button and to have user submissions appear below the box as they are submitted.
如果可能的话,进行此操作所需的脚本遍历或讨论将是很棒的.
If possible, a walk through or discussion of the script needed to do this would be great.
非常感谢
推荐答案
您需要的是带有SQL查询的服务器端脚本,该查询将返回较新的帖子. 让您的javascript存储日期或上一个帖子ID的变量(用于澄清的PHP):
All you need is a server-side script with an SQL query that would return newer posts. have your javascript store a variable of the date or of the last post id (used PHP for clarification):
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);
现在您有一个服务器端脚本,该脚本将返回新帖子,因此您所要做的就是为more按钮编写javascript函数:
now you have a server-side script that will return new posts, so all you have to do is write javascript function for the more button:
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;
}
}
}
现在要发布新条目,请使用您喜欢的语言创建服务器端脚本来处理新帖子:
now for posting new entries create a server-side script of your favorite language that handles new posts:
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
}
});
}
现在将文档完全加载后,将功能绑定到适当的按钮上:
Now bind the functions to the appropriate buttons when the document is fully loaded:
$(document).ready(function (){
$('#moreButtonId').click(updatePosts);
$('#submitButtonId').click(submitPost);
});
这篇关于如何从用户提交的帖子中制作提要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!