Asked  5 Months ago    Answers:  5   Viewed   531 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.

Monday, July 19, 2021
answered 5 Months ago

dataType - delete this one.

Add console.log and open console in Your browser

success: function (data) {
   console.log( data );

show Your console, and then You will see why. Maybe an unwanted char or php error

Second thing - there should be if stament like this (I supposed)

if (data == "1") // it is returning string, not integer.

You can also try to use switch case in success.

Wednesday, March 31, 2021
answered 9 Months ago

enter image description hereAfter some test's, i thing the problem are the ++ in the url, try to encode:


as this worked for me:

for ($x=0; $x<=10; $x%2B%2B)
  echo "The number is: $x <br>";


+ = %2B

the server got this: ($x=0; $x<=10; $x ) so it is infinite loop, as $x stays 0

Wednesday, March 31, 2021
answered 9 Months ago

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.

Tuesday, August 17, 2021
HuLu ViCa
answered 4 Months ago

there have been 2 problems in your PHP script:
1. the function was not called
2. the return-value of the function was not echoed.

(in order to respond to an AJAX call, the called url always needs to output something - JSON in the most cases. AJAX can't access PHPand thus can't read PHP return values.)

here is the fixed version:

function update_this_func($remove_option, $uservideo_id){

    global $wpdb;

    $wpdb->query("UPDATE " . $wpdb->prefix."uservideo
                  SET is_removed =" . $remove_option . "
                  WHERE uservideo_id =" . $uservideo_id );

    return json_encode(['status' => 'Updated!']); // return status as json
echo update_this_func($_POST['remove_option'], $_POST['uservideo_id']);
Wednesday, September 1, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :