0

I have a PHP function

function ExportExcel()
{
   // code
}

and a link on the page Download in Excel

<a>Download in Excel</a>

So what I want is when users clicks on that link, PHP function would be called and data will be downloaded in excel.

I may need to Ajax for that. How do I go about doing that ?

skos
  • 4,102
  • 8
  • 36
  • 59
  • what kind of data you exporting to excel? is it xml, sql, etc? and where the data is coming from? – Elen Mar 13 '12 at 10:01
  • You can always convert the function into a PHP script and then have the link point to that script... – seeming.amusing Mar 13 '12 at 10:01
  • @Robus yes I did, but still unsure on how to do it – skos Mar 13 '12 at 10:02
  • @Elen well.. the function works fine. I haven't posted the entire function code here with parameters and all to save time. – skos Mar 13 '12 at 10:03
  • @Robus why would you need jQuery for this? All you need is a link to the PHP script generating the Excel file. See http://stackoverflow.com/questions/3269345/how-to-generate-an-excel-document-with-multiple-worksheets-from-php – user247702 Mar 13 '12 at 10:10
  • @Stijn: I misread OP's post (read 'may need' as 'need'). Of course that's the best way to go about it – Robus Mar 13 '12 at 10:11

5 Answers5

1

here is the function i implemeted recently:

$('#toexcel').live("click",function() {
            $.ajax({
                url: "toExcel.php",
                data: "sql="+encodeURIComponent(sql),
                beforeSend: function(){
                    $("#wait").show();
                },
                complete: function(){
                    $("#wait").hide();
                },
                success: function(response){
                    window.location.href = response.url;
                }
            });
        });

where sql variable actually stores sql query to the server, and then toExcel.php if getting passed sql, submitting it to the server and outputs the result using PHPExcel() object.


EDIT

i think i understood what you trying to achieve. your ExporExcel() function already outputs the results you need, right? is so, then you can do it as follow:

$('#toexcel').click(function() {
            $.ajax({
                url: "toExcel.php", // should contain and _call_ you ExportExcel() function
                beforeSend: function(){
                    $("#wait").show(); // this is loading img to show
                },
                complete: function(){
                    $("#wait").hide(); ;// this is loading img to hide once complete
                },
                success: function(response){
                    window.location.href = response.url;
                }
            });
        });
Elen
  • 2,345
  • 3
  • 24
  • 47
1

first let me make sure you know php is only parsed when the page is first being distributed. If you click a link on the page, it has no idea the php function on the same page exists because the function only existed server-side while the code was being parsed. That being said, you can easily make a separate page called download.php and call your function on that page. Then your link can just link to that page.

If you want your custom download page to return to the user as an excel file, you can use custom php headers to convince the browser that it is downloading an excel file. (you'd have to specify the MIME type for excel files)

edit: this would cause a download to start of an excel file created by your function call and activated by your link click. You don't need any JS or JQuery for this.

edit2: here's example code for the download file to get you started

<?php 
header("Content-type: application/excel");
print($data); /* print out the contents of the excel file here */
exit();
?>

If you do it like this, your php page will not redirect from your original page, but will bring up a download box from the browser instead. If your using csv files instead of xls files, you'll need to change the mime type.

user980058
  • 511
  • 7
  • 18
  • But that will take user to another page... I'd like the download to start while user stays on the same page. – skos Mar 13 '12 at 10:17
  • If done right, the browser will offer to open or save the Excel file and stay on the same page. – user247702 Mar 13 '12 at 10:59
1

You could possibly just use a GET statement, so it would look something like this...

HTML

<a href="download.php?init=true">Download in Excel</a>

PHP

function ExportExcel()
{
    // code
}

if($_GET['init'])
{
    ExportExcel();    
}
PapaSmurf
  • 1,035
  • 2
  • 14
  • 26
0

Just provide link of that excel file in href of anchor , browser will download automatically If your file form DB then providelink of excel.php , and in excel.php do processing of getting excel file and creation of it . read this artical..do like that

sandeep
  • 2,244
  • 1
  • 23
  • 38
  • the file doesn't really exists on server. There are Database records that I need to export in Excel – skos Mar 13 '12 at 10:05
0

you can handle the request in your js scrpit file

$("a").click(function(){
 jQuery.ajax({
    url: "path/to/controller",
    type: "POST",
    dataType: 'json',
    data: {'mentod':'ExportExcel'},
    success: successCallback,
    error:failureCallback
 });
});
palAlaa
  • 9,500
  • 33
  • 107
  • 166