I need to export data from a database through a webapp. And when I call the function with ajax post method. The file is created successfully on the server, but after that it is not downloading it, the download function is not called, somehow, or it is not working. I don't know the reason, and I don't get any error messages.
Here is my jQuery function that sets post data for the php:
$(document).ready(function(){
$('#exportCSV').click(function(){
var ajaxurl = 'admin.php?page=site&edit=false',
data = {'export': 'csv'};
$.post(ajaxurl, data, function (response) {
});
});
});
A switch that runs the selected file export function, than tries to download it
switch ($_POST['export']) {
case 'csv':
$query = $_SESSION['query'];
$FName=exportToCSV($conn,$query,$fName);
download($FName);
break;
}
And the download script that is not called, or is not working correctly
function download($file){
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: ".filesize($file));
header("Content-Type: application/csv");
header("Content-Transfer-Encoding: binary");
readfile($file);
}
And this is how this "site" is called. Everything above is in the "somesite.php" file
if(isset($_GET['page'])){
switch ($_GET['page']) {
case 'site':
include("somesite.php");
break;
}
}