在表单提交和重定向之间显示“请等待”

I have a page that contains an HTML form that submits back to itself once the user clicks a link in a list of returned search results. Once they click the link, the page takes the submitted variables, runs a bunch of searches on various external APIs, parses a bunch of data, and adds a bunch of stuff to the database, then redirects to a new page that has been created from that data.

All the searching and parsing can take up to six or seven seconds; I'd like to be able to show the user a "Please Wait" kind of message while all that work is happening behind the scenes.

Trouble is, I can't show and hide a DIV because it will screw up my PHP redirect if I've already generated output before the

header('Location: ' . $newURL);

command. I've searched around for answers but while there are many that are similar, none of them are close enough to my specific situation that I can hack around them.

I'd be grateful if someone could point me in the right direction.


Updated version which now works, courtesy @Izkata from his comments below:

jQuery("a").bind('click', function() {
  jQuery('#feedback')[0].innerHTML = 'Searching, please wait...';
})

Turned out what I needed to do was assign bind a the message to the click of a link, not to 'submit', as submit was looking for form data.

The simplest way I can think of doesn't require the server to do anything:

<div id='wait_message' style='display: none;'>
   Please wait while search is in progress...
</div>

...

$$('.links').observe('click', function(e) {
   $('wait_message').show();
});

(Event is written using Prototype.js; you should use whatever is appropriate (JQuery/mootools/etc))


Using the example page in the comments, this works - it runs in Firebug, so just putting it on your page somewhere should work just fine.:
jQuery('#newMovieSearchForm').bind('submit', function() {
   jQuery('#feedback')[0].innerHTML = 'Searching, please wait...';
})

There's probably a jQuery-way to update the text instead of using innerHTML, but I don't know it - we don't use jQuery here.

You are right, you won't be able to output data to the screen and then try to redirect afterwards using PHP. You could accomplish this by echoing JS:

echo 'Please wait...';
// Time-intensive PHP here
echo '<script>window.location = "new-location.php";</script>';

You can do something like this:

First, take care of output buffering, i.e. you want php to display output as it executes - you don't want it to buffer.

That way, you can output some html that will show a "loading.." sort of thing.

And, will wait for the script to end it's execution.

Once, you are done with php, redirect using a meta tag, something like:

echo '<meta http-equiv="refresh" content="5;URL=\'http://example.com/\'">';