问题
I'm have a PHP and MYSQL based newsfeed. To get new content, the user has to refresh the page. I'm working on a push system that would update the element with new posts without refreshing the whole page. How would I go about that?
回答1:
Sounds like a job for Ajax!
Also if you want to make the work really easy, use jquery. The Syntax would look something like this:
$.ajax({
type: "post", url: "/SomeController/SomeAction",
success: function (data, text) {
//....
},
error: function (request, status, error) {
alert(request.responseText);
}
});
EDIT
to have the page update when a user clicks on a "show more content" link you can either use javascripts onclick function or use jquery's built in stuff.
Regular JS:
<somekindOfLinkOrButton onclick("moarStories();")/>
<script>
function moarStories(){
//ajax here
};
</script>
Jquery way (much eaiser)
<script>
$("#ElementId").click(function(){
//ajax here
});
回答2:
You just send a request to the server and server tries to keep the connection alive until you get new update. When you get new update from the database, you just push it to the client and close this connection
来源:https://stackoverflow.com/questions/10439557/php-newsfeed-with-reload