2

So I have a page where the user will hit the "export" button and I want to run a javascript function and get the data I need for the export and then call a php script to get the data from my database and force a download. Right now the problem is, I can run the javascript and then I call a post to my script, but nothing is downloaded. I am assuming it is because I am not actually moving the window to the php file, I am simply calling the script. can anyone help me out?

javascript: ids is an array that I need to pass to get the information. This is inside the function taht I call when the user hits the "export" button.

$.ajax({        
   type: "POST",
   url: "exportLeads.php",
   data: { 'idArray' : ids }
}); 

php: I used this Export to CSV via PHP quetsion. The function are implemented properly and I get no errors. There is just no file that is prompted for download.

$data = mysql_query($query) or die($query.mysql_error());
$results = array();
while($line = mysql_fetch_array($data, MYSQL_ASSOC)){
    $results[] = $line;
}
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
echo array2csv($results);
die();
Community
  • 1
  • 1
ageoff
  • 2,798
  • 2
  • 24
  • 39
  • You don't use ajax to download files. – Musa Jul 06 '13 at 18:42
  • @Musa Right, my question is, how do I maybe open my script in a new window or make it so the user is prompted for the file download. I need to use ajax to send my data to the script is all. – ageoff Jul 06 '13 at 18:46
  • how about setting `window.location.href` to the url with the data as the query string. – Musa Jul 06 '13 at 18:51

3 Answers3

1

Got no time to write code, but in few words, you need:

  • Execute JS to call server side via ajax.
  • Generate file, put it to the server drive with some random name. Remember that name to session. Send some response to client.
  • When client gets response, redirect user with JS to some page where PHP code checks if there is some filename in session, and if there is, reads this file, deletes it and its name from session.
Bogdan Burym
  • 5,482
  • 2
  • 27
  • 46
1

You cannot download files with ajax. You can use window.location.href to redirect user to the file location, but whatever, i suggest you to read this, someone has a better answer for you :)

Download a file by jQuery.Ajax

or you can also use something like this in your javascript

$.post( "exportLeads.php", { 'idArray' : ids }, function( response ) {
  if ( response.error === 0 ) {
    window.location.href  = response.location;
  }
}, "json" );

and in your php file

<?php
  if ( $has_error ) {
    echo json_encode( array( "error" => 1 ) );
  }
  else {
    echo json_encode( array(
      "error"     =>  0,
      "location"  =>  "link_to_file"
    ) );
  }
?>
Community
  • 1
  • 1
bystwn22
  • 1,776
  • 1
  • 10
  • 9
0

You could go for window.location.href in your success function:

$.ajax({
     type: "POST",
     dataType: "json",
     url: "exportLeads.php",
     data: { 'idArray' : ids },
     success: function(data) {
          if (data.success) window.location.href = "/link-to-your-download-script";
     }
}); 

You would have to change the response to json_encode and split the functionality into two scripts, one for the export and one for the download script (with a unique id or sth.)

Jan
  • 42,290
  • 8
  • 54
  • 79
  • For some reason, this never gets into the if statement. Do I need to be returning something in my php script? – ageoff Jul 06 '13 at 19:24