By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,064 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

AJAX & PHP - generate report & force download?

P: n/a
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>

Sep 10 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
If the goal is to send the user a pdf file,
perhaps having the php script output a content-type
of "application/octet-stream" as opposed to "text/html"
would be what your really after.

This should allow the download to begin w/o disturbing
the current display at all.

k.****@worldnet.att.net wrote:
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>


--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML/CSS,Javascript,TCP ...
--`
Sep 10 '05 #2

P: n/a
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.

Sep 11 '05 #3

P: n/a
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.

Sep 11 '05 #4

P: n/a
k.****@worldnet.att.net wrote:
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.


At least you managed to find a solution and thats whats important.

I wonder why it did not work, as I use the same methodology
on my site to force downloads of all sorts of files,
even text files.

Not exectly the same I guess , as I use a C++ .cgi as opposed PHP
to output the content-type:application/octet-stream header , but
still leaves me curious none the less.
--
--.
--=<> Dr. Clue (A.K.A. Ian A. Storms) <>=-- C++,HTML/CSS,Javascript,TCP ...
--`
Sep 11 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.