Question

say, my PHP script needs some time to calculate its operations. I need to keep the client informed about the operation progress. An example can be PHP based file download, where I need to provide to client estimated time needed to finish the download, and the amount of data that needs to be copied.

In PHP I can calculate all the needed information. However, how to regularly update the client with this info?

Ideally, I would like to dynamically update JavaScript variable from within my PHP script. I read that this is not possible, so what options do I have then?

Possibilities that come to my mind:

  • Should I do regular AJAX calls from within my JavaScript into the PHP script to get the progress info?
  • Should I study for me yet unknown COMET methodology

Do the two above possibilities make sense? Is there any other more practical solution available?

Thank you in advance.

Was it helpful?

Solution

Yes, both of those options make sense.

There are lots of different COMET-style techniques, but one of the simplest is a long-running iframe. You have a PHP page that monitors the progress of the operation, and outputs something like this at regular intervals:

<script>parent.updateProgress(relevantUpdateInfo);</script>

...where relevantUpdateInfo is the information about the progress, and updateProgress is a global function on the page containing the iframe that shows the updated information in the UI.

When outputting the script tags, be sure to flush the output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top