Jump to content

displaying intermediate page / message during wait times


flyingboz

Recommended Posts

My script needs to process a credit card number, it may take 1 to 10 seconds to get a response.

I would like to display a "Processing, Please wait message" immediately upon the submit, and then launch
a new page based on the results, assuming they arrive.

If I flush the buffer, I can get the "processing" message to the browser, but the results , when echoed, end up on the same page.

If I don't flush the buffer, then I have this blank screen and a wait while the user sees nothing until my response comes back.

Thoughts and suggestions are welcome.
Link to comment
Share on other sites

I'll take a stab at one way of doing this.  I have no idea if there is a better way though.

This method depends on having PHP fire off a background script that will enable control to be returned immediately to the current script.  Taken from the PHP manual:
http://www.php.net/manual/en/ref.exec.php
PHP at jyoppDot-com wrote:
[i]Here's a function to launch a low-priority background process very simply, returning control to the foreground.  I found that a lot of the other approaches shown here allow the script to continue running, and the page is served up to the user, but the PHP thread that launched the process cannot be re-used until the process has completed.  Stdout and stderr need to be redirected to /dev/null to prevent this.

This is for BSD/*NIX servers only (tested on Fedora Core and OSX).  Win32 approaches would be necessarily very different.  Omit 'nice' if you don't want to run the process with lowered priority.
[/i]

[code]<?php
function fork($shellCmd) {
   exec("nice $shellCmd > /dev/null 2>&1 &");
}
?>[/code]

This also depends on javascript being able to check for a file's existence on the web.

Here would be the steps:
1) User fills out a form and submits with their credit card info.

2) Processing script will generate a unique ID for the request.  Fire off a background script that takes the unique ID for the request in addition to any other details that actually processes the request.  Control will be returned immediately to the form handler which will output a simple page with javascript.

** Now we have two separate things happening **
A) The form handler will echo out it's page containing javascript that shows some sort of progress bar.  In addition, the javascript will continuously check for the existence of a file unique to this particular request on your web server.  As soon as the unique file for the request is found on the webserver, redirect to the final processing / confirmation page.

B) The background script is validating the credit card.  As soon as it's valid and everything is ok, create the unique file so the client's javascript can terminate executing and redirect.

If anyone has a better idea I'd be glad to hear it.  I know nothing about it, but this might be better handled with Ajax?  Again, I have no clue about that.
Link to comment
Share on other sites

Thank you for the reply.  It appears to be well thought out, and one way of successfully approaching the problem.  I am not allowed to depend on javascript, however, as it can be (and often is) turned off by the user. 

I need to be able to display a progress bar (currently using an animated gif to 'fake' it)...and then force a page load when the results are in without running code on the client.

Link to comment
Share on other sites

AFAIK you can't do this without javascript (or possibly flash or AJAX).  PHP runs entirely on the server which means you can't write a script that sends output to the client and then processes something on the server while updating the client page.  You can only accomplish this with code that runs on the client to the best of my knowledge.

You can perform a test to determine if the user has javascript enabled.

Add something like:
<div id="jscript_test">
  <input type="hidden" name="jscript_test" value="false" />
</div>

to the form.  Add javascript to the page's onload function that changes the contents of that div tag to:
<input type="hidden" name="jscript_test" value="true" />

The div tag's contents will only change if javascript is turned on.  Then on your form handler you can check if the value of $_POST['jscript_test'] is true or not.  If it's true you can use the flashy progress bar method I described above.  If it's false just do all of the processing on the server and the client can wait until it finishes.
Link to comment
Share on other sites

[color=green]Quote: AFAIK.....PHP runs entirely on the server which means you can't write a script that sends output to the client and then processes something on the server while updating the client page. [/color]

I don't need to continually update the client page.  I just need to:

1) echo the "please wait" stuff to the browser  (done)
2) process the credit card payment                (done)
3) load a NEW results page.                                  [color=red](not done yet)[/color]

Where I'm getting hung is how to force a a new page load after having executed the flush() to write the buffer.
I can keep on echoing to the same page, but I need to get rid of the "please wait" message that was already sent as part of (1). 

As a picky point of nomenclature, and not to be rude , it is entirely possible in PHP only to append to the page using the output buffering and flush functions; this could be considered "updating", though not in the classical javascript sense.


Link to comment
Share on other sites

Well then, I'd say you're stuck.  If you've already output something to the client, you can't erase it.  So if the page says "Please wait..." it's not going to go away until you redirect to a new page.  And since you've already output something, you can't redirect through PHP with a call to header as the headers have already been sent.
Link to comment
Share on other sites

Well, rats.

I was hoping that there was a method that I was unaware of that would let me force the php script to initialize another page after a buffer flush.  I presume that the http_request() functions would suffer from the same issue as the header() redirect?

I guess I could do a "click here to see the results", but that seems awfully cheesy.  Probably better to have a warning "this could take a while" and a blank page than make the user click a link to see the results.

Thanks for your time.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.