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 ?

6
  • what kind of data you exporting to excel? is it xml, sql, etc? and where the data is coming from? Commented Mar 13, 2012 at 10:01
  • You can always convert the function into a PHP script and then have the link point to that script... Commented Mar 13, 2012 at 10:01
  • @Robus yes I did, but still unsure on how to do it Commented Mar 13, 2012 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. Commented Mar 13, 2012 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 stackoverflow.com/questions/3269345/… Commented Mar 13, 2012 at 10:10

5 Answers 5

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();    
}
Sign up to request clarification or add additional context in comments.

2 Comments

i prefer this simple idea, but the PHP is unsafe... use isset to check $_GET['init'] exist or not first.
I'd agree, just a quick demo :)
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;
                }
            });
        });

2 Comments

it means I'd need to create an addition file (toExcel.php), right?
well.. you can just move your ExportExcel() function there
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.

2 Comments

But that will take user to another page... I'd like the download to start while user stays on the same page.
If done right, the browser will offer to open or save the Excel file and stay on the same page.
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
 });
});

2 Comments

can you please explain what is url: "path/to/controller"
It means the url of the page that handles the request, write the same URL you write in href inside <a> tag.
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

1 Comment

the file doesn't really exists on server. There are Database records that I need to export in Excel

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.