PHPExcel download using ajax call
add target=_blank in your ajax success function like below
success: function(){
window.open('http://YOUR_URL','_blank' );
},
otherwise you can handle smartly to open your Excel download link in new tab with jQuery trigger function or etc.
Download a file with an ajax call
Found a way to do this, although I'm not sure if this is an ideal approach.
I added a hidden iframe in the page. When the ajax call returns, it returns the url of the created data. I used javascript to redirect the iframe to that url which automatically triggers the download action.
Download excel file with ajax call
I solved my issue by following way.
add target=_blank in your ajax success function like below
success: function(){
window.open('http://MY_URL','_blank' );
},
On success open new window with my_url.
Codeigniter PHPExcel Download via ajax call
I solved my problem by tweaking a little bit of RatHat code answer and this is what saves me:
$('#btnExport').click(function(){
var fdate = $('#dateFrom').val();
var tdate = $('#dateTo').val();
var dept = $('#txtdept').val();
var item = $('#txtsearch3').val();
var form1 = $('#formData1');
var form2 = $('#formData2');
$('#txtdept').val(dept);
$('#txtfdate1').val(fdate);
$('#txtTdate1').val(tdate);
$('#txtItem').val(item);
$('#txtfdate2').val(fdate);
$('#txtTdate2').val(tdate);
if(fdate == "" || tdate == "")
{
$('#no-dates').modal('show');
}
else if(fdate == "" && tdate == "")
{
$('#no-dates').modal('show');
}
else
{
if(dept != "")
{
form1.attr('action',"<?php echo site_url('tms/exportToExcel');?>");
form1.attr('method','POST');
form1.submit();
}
else if(item != "")
{
form2.attr('action',"<?php echo site_url('tms/exportsolo');?>");
form2.attr('method','POST');
form2.submit();
}
}
});
Thanks to all who leave comments to my post....
Passing data from PHP class to PHPExcel via AJAX
A general idea of what you could do:
Create a button e.g.
<a href="#" id="export">export to Excel</a>
Then in jquery you have to create something like:
var grid = $(".grid.digital_edit").loadGrid({...}); //or similar - what you did to load the data into the grid
$('#export').click(function() {
$.ajax({
url: "export_to_excel.php", // the url of the php file that will generate the excel file
data: grid.getData(), //or similar - based on the grid's API
success: function(response){
window.location.href = response.url;
}
})
});
The file export_to_excel.php will contain the code that generates the excel file:
- This is where you'll initiate the PHPExcel class and create a file e.g. new_excel.xls
- In your response array the $response['url'] will contain the absolute url to the newly created file. (http://www.example.com/files/new_excel.xls)
It may sound too complex, but try to separate your goals and achieve one at a time. E.g.
- Create the button.
- Then try to make a simple AJAX call when hitting the button.
- Then create your export_to_excel.php file and try to work with the PHPExcel class.
- Create a sample excel file based on the tutorials found.
- Create an excel file based on your own data, but hard-coded in the php file.
- Create the correct AJAX call that sends the wanted data to a php file.
- Catch the correct AJAX call.
- Pass the data from the AJAX call to the PHPExcel class.
- Create the excel file.
- Send back the url to the excel file.
- Redirect the user to the url of the excel file.
EDIT
To help you a bit more: You need only one PHP script/file. The same one will receive the AJAX call from the javascript file, will generate the excel file and will return/respond the file url to the javascript file(in that order). A simplified example would be:
<?php
//export_to_excel.php
$data = $_POST['data']; // get the data from the AJAX call - it's the "data: grid.getData()" line from above
//... format the received data properly for the PHPExcel class
// later on in the same file:
$xls = new PHPExcel();
$xls->loadData($formattedData); //I assume that there is a similar loadData() method
$xls->exportToFile('/vaw/www/example.com/public/files/new_excel.xls'); // I assume that there is an exportToFile() method
$response = array(
'success' => true,
'url' => 'http://www.example.com/files/new_excel.xls'
);
header('Content-type: application/json');
// and in the end you respond back to javascript the file location
echo json_encode($response);
And then in javascript you display the file with this line
window.location.href = response.url; //response.url is $response['url'] from the PHP script
Related Topics
How to Replace the Deprecated Set_Magic_Quotes_Runtime in PHP
Does the Condition After && Always Get Evaluated
What Does [\S\S]* Mean in Regex in PHP
How Does {} Affect a MySQL Query in PHP
PHP Eval and Capturing Errors (As Much as Possible)
Php: Trying to Create a New Line with "\N"
One Form with Two Submit Buttons and Different Actions for Each Button
Upload Multiple Files to PHP Server Using Curl Command Line
Mysql_Connect VS MySQL_Pconnect
Utf-8 in PHP Regular Expressions
Error Checking for Pdo Prepared Statements
Efficient Way to Pass Variables from PHP to JavaScript
Rendering an Svg File to a Png or Jpeg in PHP
PHP Call Class Method/Function
Refresh a Part of Webpage in PHP