Viewed   1k times
App::import('Vendor', 'PHPExcel/Classes/PHPExcel');
$objPHPExcel = new PHPExcel();
header('Content-Type: application/');
$file_name = "kpi_form_".date("Y-m-d_H:i:s").".xls";
header("Content-Disposition: attachment; filename=$file_name");
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

When I call above code directly from the browser, the result file is downloaded. But if I make an ajax call to above code, I don't get the download prompt. I can see from console tab that the ajax call was successfully completed and a bunch of random characters is seen in the response data. I'm assuming that is the excel object.

Does anyone know how I can achieve the download excel feature using ajax? I don't want to refresh the page. When the user clicks on the "export" button, there should be an ajax call to the php file and prompt the user to download.



add target=_blank in your ajax success function like below

success: function(){'http://YOUR_URL','_blank' );

otherwise you can handle smartly to open your Excel download link in new tab with jQuery trigger function or etc.

Saturday, December 10, 2022

I think you've got almost everything. The callback function you have under success needs an argument which stands for the results from search.php

     success: function(res) {
             $('#result').html("<br><br><br><br><br><br><br><br><br><br><div class='center'><img src='img/loader.gif' /></div>").hide().fadeIn(2500, function() {
                 $('#result').html(res + "<br /><br /> Finished");

res is everything outputted be search.php. Echo, stuff outside of php tags, etc Anything you'd see if you loaded search.php itself.

I don't know if you wanted 'Finished' to still be there. Take it out if you dont.

Tuesday, October 18, 2022

Putting elements in a form and submitting them just seems to hacky, and I don't feel comfortable using it.

When you are using these types of things I think you are risking a bit, the next versions of browsers may simply not support them.

Besides I am passing complicated arrays as options in my ajax call to the server, and it's not easy to convert them all into an html form, unless I serialize the arrays in a hidden element and unserialize it on the server side, but that's all too complex.

What I did instead was, when the ajax call is made, the server stores the output in a session, then it returns a unique key for that value, another page on the server will simply echo the output when that key is given to it as an input,

So user clicks on something, then an ajax call is made, then the server stores that in a session, then user clicks on a download link and then the server removes that session.

It may not be the most perfect solution specially since the user has to click twice, but it just seems more standard to me.

Saturday, September 17, 2022

You can do this easiest (and possibly only) server-side, no need for ajax, like this:

header('Content-type: "text/csv"; charset="utf8"'); //adjust encoding if needed
header('Content-disposition: attachment; filename="fileNameHere.csv"');
//output document in response

Someone feel free to edit this if the syntax if off, it's been quite a while since I've had a php project.

Thursday, August 25, 2022

i think your problem may be in ajax code since you are using formData object . try append the message variable with it

$('#submit').on('click', function(){

  var fd = new FormData(this);
  fd.append('message ',$('#message').val());

    url:"<?php echo site_url('home/send_chat');?>",    
    data: fd,  
    cache: false,
    contentType: false,
    processData: false,   
    success: function(data){                 
    error: function(xhr, status, error) {
Saturday, November 12, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :