Skip to content
Advertisement

Infinite Scroll Pagination using PHP and jQuery returns only few posts and halts with a loader gif

I m trying to replicate code found online for Infinite scroll using PHP and JQuery.

But unfortunately, there is a flaw in the code which makes it only to return 7 posts only and stops fetching other posts upon scrolling down leaving a loader gif at the bottom.

Generally, I wouldn’t have asked this question, but the code seems pretty nice (in laymans terms) which I presume will be very helpful for rookies like me in the community.

Meanwhile will search other resources and try to answer it by myself.

My code Goes as :

Index.php

JavaScript

getMoreData.php

JavaScript

Any help is greatly appreciated.

Advertisement

Answer

I would set this up differently using classes and controllers etc, but as simple scripts, I might set it up something like:

Create a file called getData.php with this content:

JavaScript

Create another file called index.php with this content:

JavaScript

Now, I cant test this locally so there might be an error or two in there, but that should give you the general idea.

One thing im not 100% sure of is that if ($(window).scrollTop() == $(document).height() - $(window).height()){ condition.

XSS warning

You dont show how these “posts” get added to the database, presumably they come from user submissions on some other form. If that is the case, make sure that you understand what XSS vulnerabilities are and how to prevent them

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement