AJAX & PHP - generate report & force download?

K

k.mitz

Hi,

I have a PHP application that allows users to generate a .pdf report of
their database content. Normally, I've had to refresh a page to call
the script to generate the report, so there's a second or so when the
browser goes blank. I was wondering if it was possible to use AJAX to
call the script to generate the report, then begin the download without
refreshing the page (or in the case of I.E., leaving me with a blank
window that you have to back out of).

The following code 'works', in that it inserts the pdf code in the
textarea, but I'd want to force the browser to start a file download
without refreshing the page.

BTW, I'm using the Prototype 1.3.1 library.

<script>
function searchSales(rptID)
{
//generates the report
var url = 'http://192.168.1.128/sendReport.php';
var pars = 'rptID=' + rptID;

var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars,
onComplete: showResponse} );

}
function showResponse(originalRequest)
{
//put returned XML in the textarea
$('result').value = originalRequest.responseText;
}

</script>
<a href="#" onclick="javascript:searchSales('1034')">Click</a>
<textarea id=result cols=60 rows=10 ></textarea>
 
K

k.mitz

Well Dr., I'm using the PEAR HTTP_Download class, and the content type
is set as 'application-pdf', as shown below. But, what the heck, I
tried octet-stream. Same results.

I've also tried the location.href = 'test.pdf' code, which works fine
for a static file, but I need to generate the report first (it changes
now and again).

Obviously, I can get it to work if I load the page with the code shown
below, but I'd like to keep the user on the original page, which in my
limited understanding is what AJAX seems to be about...

//PHP code

include('HTTP/Download.php');
$dl = &new HTTP_Download();
$dl->setFile('/tmp/test.pdf');
$dl->setContentDisposition('inline', 'test.pdf');
$dl->setContentType('application/x-pdf');
$dl->send();

PS I've also set the content disposition to attachment.
 
K

k.mitz

Thanks for the prescription, Dr. I tried it, but the result was the
same.

//PHP code

include('HTTP/Download.php');
$dl = &new HTTP_Download();
$dl->setFile('/tmp/test.pdf');
$dl->setContentDisposition('attachment', 'test.pdf');
$dl->setContentType('application/x-pdf');
$dl->send();

Here's what actually ended up working...I changed the first function to
this:

//Javascript

function searchSales(rptID)
{
var url = 'http://192.168.1.128/sendReport.php?rptID=' + rptID;
location.href = url;
}

I'm sure I tried that before...Oh, well.

Thanks for your post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top