PHP Newsfeed with Reload

风流意气都作罢 提交于 2019-12-11 05:49:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!