Is it possible to POST using AJAX and then force download with the response when successful?
My code:
$(document).on("click", "#CSV", function () {
var csv_value = $('#result').table2CSV({
delivery: 'value'
});
console.log(csv_value);
$.ajax({
type: "POST",
url: "csv.php",
data: {
csv: csv_value
},
success: function (html) {
//Download
}
});
});
As you can see i'm converting my HTML
table to CSV
then posting it to my php
file where I want to generate a .csv
file as this is not possible using JS.
php script.
$file = $_POST['csv'];
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
$csv_output=stripcslashes($_POST['csv']);
print $csv_output;
exit;
Can I initiate a download from my success function?