473,573 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AJAX & PHP - generate report & force download?

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(rpt ID)
{
//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(or iginalRequest)
{
//put returned XML in the textarea
$('result').val ue = originalRequest .responseText;
}

</script>
<a href="#" onclick="javasc ript:searchSale s('1034')">Clic k</a>
<textarea id=result cols=60 rows=10 ></textarea>

Sep 10 '05 #1
4 9545
If the goal is to send the user a pdf file,
perhaps having the php script output a content-type
of "applicatio n/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(rpt ID)
{
//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(or iginalRequest)
{
//put returned XML in the textarea
$('result').val ue = originalRequest .responseText;
}

</script>
<a href="#" onclick="javasc ript:searchSale s('1034')">Clic k</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
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->setContentDisp osition('inline ', 'test.pdf');
$dl->setContentType ('application/x-pdf');
$dl->send();

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

Sep 11 '05 #3
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->setContentDisp osition('attach ment', '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(rpt ID)
{
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
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->setContentDisp osition('attach ment', '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(rpt ID)
{
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:applicatio n/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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1957
by: Mark Thiel | last post by:
I am working on a project for my company, which has a structure of six major areas. Certain people need to have access to all of the data, whereas other people only require access to the information for one area. Since the users do not know Access, I created a form to make it easy to generate a report. Is it possible, either when the...
0
2247
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional assistance. I say additional because I've already had help which is greatly appreciated. I do try to take the time and understand the provided script...
13
1712
by: Sharon | last post by:
Hi all, I was wondering if anyone had experience using ajax with asp.net controls. What i'm looking for, are ways to implement and possible problems. A way to implement could be rendering the control on the server side, and send the html string back to javascript. A possible problem is the control post back behavior and control view state...
31
3100
by: Tony | last post by:
I just noticed that prototype.js is one of the files in the Ajax.NET distribution - I'm pretty concerned about this. Does anyone know if this is the same "prototype.js" that is not well-liked around here? If so, do you know if Ajax.NET can be used without prototype.js? -- "The most convoluted explanation that fits all of the made-up...
1
5697
ak1dnar
by: ak1dnar | last post by:
Hi i need to download some files (.pdf/.doc) from web root. I am using a ajax script to send the file IDs and PHP will process it and it should generate the save as dialog box. Currently without ajax when i process PHP file i can open the download box to save the file. example: http://localhost/website/download.php?fid=1005 but when i am...
3
1889
by: vunet.us | last post by:
Hello, I am breaking my head running out of ideas about the best solution to my goal. I want to load some pages generated with the server (ASP) and assign their html results to JavaScript, so users could click the links and the content of any selected page would show up immediately, making no requests. So, I have one main page called...
2
4250
by: Nathan Sokalski | last post by:
I am moving my website from my machine to my webhost, and need some help with what extra files I need to include due to the fact that I used AJAX in my site. Everything on the site is obviously functioning as desired when I test it on my machine using Visual Studio 2005. From what I have determined, the files I am forgetting are the JavaScript...
10
3027
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
controlsPlease could some of you here post some of your live examples of AJAX (esp drag panels, collapsable panels, and popup menu.) (It's one thing to talk about how great something is, but it's another to really see how great something is.) Thanks -- Share The Knowledge. I need all the help I can get and so do you!
4
2189
by: sheldonlg | last post by:
I haven't received an answer with my other post, so I am rephrasing it here. In php I have a 2D array which I "print". The headers force it to be a file on the user's system. The user receives a pop-up that asks him if he wants to open it or save it. (Of course, he can simply cancel). The file I send is in an Excel readable file. This...
0
7781
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7788
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8066
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5292
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.