PHP Loading GIF while PHP page loads

Hello, I have a PHP script which runs for around 10 seconds during that time the page is still loading and page shows nothing. I want to show a loading gif or something like that in the meantime .Any ideas ? Thanks.

Open a page that shows the GIF, then have that page redirect to the one that takes ages to load?

1 Like

I am using this command for redirect window.location.assign();.
and this inside style :

body {
background-image: url(“loader.gif”);
background-repeat: no-repeat;
}

The loader does not appear and it redirects directly

You must do that after the page has finished loading, that is after the window onload event fires.

I once tried that and some browsers stopped the animation as soon as the request for the new page was sent so this might not be so easy - make sure to test this in all browsers. I don’t remember what workaround I used but maybe an iframe helped.

The most reliable way would be to display a page with the gif and then load the next page with ajax and inject the loaded content into the current page with innerHTML. But you may need to provide some alternative without scripts for people without javascript and for search engines.

if my understanding of PHP is correct you won’t be able to do what you want as PHP is a serverside script. The page is parsed before being handed to the browser so until PHP is finished with it it won’t output your page to the browser and hence any ‘loading’ script.

I assume this is relying on the gif remaining onscreen whilst the second page loads. Clever in its simplicity :slight_smile:

Bit more fiddly but i’l probably go with @Lemon_Juice, if you want to keep everything on the same page, and use ajax so your first page will load and have the ‘loading’ script run whilst ajax goes to the second page and does the PHP bit and returns it into the first page. You could do a <no script> and a direct link to the second page with a note explaining it will take ages to load.

I was thinking mainly of an old search routine that I had. The search was called from a form posting, but could take some time to complete if it had to check every record in the database. So I had bit of javascript that, on submit, would hide the search form div and un-hide another div containing the “Searching, please wait” text.

This might be relying on old browsers though - I haven’t tried it for some time. Maybe the fact it was a form submit that made it behave differently than a standard redirect.

I also have a form submit btw so I will try this one. Thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.