Rikard
Rikard

Reputation: 3

Show loading image while PHP work

I want to show an loading GIF while my PHP-script is working, small ex:

HTML CODE WITH LOADING.GIF

PHP CODE

sleep(5)
echo("<script>location.href = 'http://www.google.com';</script>");

If i run this example the page will load in 5 seconds, then show me the loading.gif in 0.3seconds and then send me to google. Is there any way to load all html to the browser then runt the PHP-script?

Upvotes: 0

Views: 1239

Answers (1)

Andy
Andy

Reputation: 17771

Due to your HTTP server's output bufferring you can't predict when different parts of your script's output will reach the browser. Instead, you should load a simple "Loading..." page and from that page use Javascript to fire an AJAX request to trigger the "working" of your current PHP script, then grab the output and replace the content of the loading page with it.

Upvotes: 5

Related Questions