0

This is a complex question so I'll try and be precise.

I've experimented with AJAX but this form requires many changes to do it. I'd like to know if there are other options.

I have a form which takes a very long time to complete. Let's say the form exists on page A. The form submits and calls page B.

Page B looks like this, and gives the incorrect result (the page appears blank while loading):

<html>header</html>
<?php
longformresult()
?>

If I setup the page like this I get the result I am looking for (the page waits until the long function is done to load the page, allowing me to display a loading screen on page A).

<?php
longformresult()
?>
<html>header</html>

however this is where things get really tricky, the longformresult() can fail and break everything below it.

Is there some way I can tell php to wait until the page is fully loaded before sending the page, allowing me to have a loading screen on page A? Or prevent a die() from killing my page? I essentially need to delay the appearance of Page B, and actually just a sleep() is probably the best approach I've had so far:

<?php
sleep(4)
?>
<html>header</html>
<?php
longformresult()
?>

Edit: Conclusion (Not the fix I was looking for though)

I used ajax and removed 'Page B' entirely.

RRRRRR
  • 31
  • 6
  • 1
    Why do you have output after ` – Barmar Oct 13 '17 at 00:43
  • Normally PHP buffers the output and doesn't send until the script is done. Is the script calling any of the `ob_XXX()` functions to flush the buffer? – Barmar Oct 13 '17 at 00:45
  • @Barmar sorry, that is pseudocode. The point was to demonstrate that I am running the long php call after the html. Is there some way I can force the buffer to flush after the long function is complete? – RRRRRR Oct 13 '17 at 16:46
  • I'm getting the feeling this is an XY problem. What does `longformresult()` do? Does it produce output, or not? If it doesn't produce any output, maybe you can run it in a separate process so it doesn't delay this page. – Barmar Oct 13 '17 at 18:10
  • The issue is caused by writing output prior to issuing the function. To resolve the issue use [`output buffering`](http://php.net/manual/en/function.ob-start.php) to store the output into memory, prior to sending it to the browser.. `....` – Will B. Oct 26 '17 at 23:21

1 Answers1

1
  1. PHP waits by default to complete the function call before proceeding (see this previous answer), so PHP necessarily waits for the page to be fully loaded before returning.

  2. AJAX would not require "many changes" if this is truly the behavior of the program. You would simply have a new file like C.php which returns the output of longformresult(), which you can later inject into your page.

Derek Brown
  • 4,232
  • 4
  • 27
  • 44
  • 1. Seems to be a different issue than the one I am facing. This is also false. As an experiment I added to the end of a file, and the beginning of my file. The former gave a white page when loading, the latter gave text. 2. AJAX is definitely looking like the best option here, but the code is finicky and not worth breaking. I will likely add a sleep. – RRRRRR Oct 13 '17 at 16:55
  • @RRRRRR what is indicates is that there is a race condition in your code. Adding a sleep might be a hacky workaround, but you should ideally use AJAX to lazy-load this data once it is available. – Derek Brown Oct 13 '17 at 17:05
  • Dependant on the web server configuration (e.g. `mod_gzip`), headers will be sent to the client prior to the output of the content (which can also be forced via [`flush()`](http://php.net/manual/en/function.flush.php)). However when using output buffering, statement 1 is true, but output buffering is not enabled by default. This results in the client receiving information from the server, then the server executing the long running function. See: [No output buffer](https://3v4l.org/gKbvj) vs [Output buffer enabled](https://3v4l.org/4BsG1) – Will B. Oct 27 '17 at 12:40