0

I am trying to display a loading message with an loading bar animation so people know the script is working away.

However even though the HTML is at the top of my PHP script , it only gets displayed once the script is fully loaded.

The script takes about 40 seconds to complete because it Generates a PDF, Inserts Data into a DB then sends a MIME email to an external MX (takes the longest part).

Here is the HTML that displays the loading message:

        <div class="grid_16">
                <fieldset>

                    <h2>
                        Processing <img src="img/loading.gif" alt="loading animation">
                    </h2>

                </fieldset>
    </div>
<?php

//rest of php page

Any idea how I display this out to the user so they don't get impatient and start hitting F5 etc.

Thanks

Merry Christmas!

3 Answers 3

2

PHP loads on the server and is then passed to the browser. This means that, until the page has completely finished loading on the server it will not be passed to the browser for display.

What you will need to do is use javascript and ajax. Firstly, remove the slow PHP from the HTML and show the page. Then once that has shown load the PHP script via ajax and once it has completed write it to the document.

Sign up to request clarification or add additional context in comments.

1 Comment

is there anyway to do this without JS, what happens if a user has JS disabled?
1

My friend, PHP only sends output when it finishes script interpretation(1). So, your option is to put the gif in the preceding page fired (using some JavaScript) with the event that moves the user from oldpage.php to slownewpage.php. So, the gif will stay in front of user until PHP sends the new HTML result and the browser has something to work with.

(1) Unless you order him to do otherwise with something like ob_flush

Comments

0

run flush(); after the part you want to directly output (within php), because you are probably buffered especially if you are gzipping your code. You can load the page like others said and pull the result with javascript as well (actually that's probably the better option).

.. can I ask why you are sending a long message during a page load? Can't you have a cron job do it (check every few seconds to see if something needs to be sent)? I think they have cron programs for windows as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.