Q:

Long delay for pdf generation & sending e-mail. Put an image while server parses scripts?

I'm working on a form in which you can order courier service. The main idea for it is to generate PDF file containing validated data and then attach it to an e-mail to client + cc for courier company.
The thing is pdf generation (TCPDF) and e-mail sending (Swiftmail) takes noticeably long.
I would like to prevent impatient user from clicking 'confirm' over and over. The ideal solution would be to show some gif 'loading' image or so. I've looked into jquery .load() function and it looks like a good thing for this problem, but what about users w/o js?
Can you point me in the right direction?

A:

I would use ajax in this case. but if lack of JS is a genuine concern for you, there are couple of ways to do this without JS. 
First you can display something to the user before starting to work with pdf. eg
<?php
echo "<html><head></head><body> Please Wait..";
flush();
// process pdf here
?>

function flush() will force the webserver to send the data to the browser, so it will be displayed while the rest of the page is loaded. However some browsers will cache this thing internally. There are ways to overcome this (check out comments in php.net for a flush() function) but it could get messy.
Another way is to process PDF asynchronously. In this case target script just displays a message that email will be send shortly. And fires the script witch actually going to send it. eg:
<?php
  echo "<html><head></head><body> Email will be send out shortly </body></html>";

  $params='';
  foreach ($_POST as $name => $value) {
    $params.=$name.'='.$value.'&';
  }
  if ($params!='')
    $params=urldecode(substr($params,0,-1));
  $parts=parse_url('process_pdf.php'.'?'.$params);
  $fp = fsockopen('process_pdf.php',80, $errno, $errstr, 30);
  $out = "POST ".$parts['path']." HTTP/1.1\r\n";
  $out.= "Host: ".$parts['host']."\r\n";
  $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
  $out.= "Content-Length: ".strlen($parts['query'])."\r\n";
  $out.= "Connection: Close\r\n\r\n";
  $out.= $parts['query'];
  fwrite($fp, $out);
  fclose($fp);
?>

in this example "process_pdf.php" will get all the same POST parameters as original script. but will be executed in the background without interrupting the original page.

