I need to convert a result of a MySQL query to an xls file, force download the file using a PHP script, and call the script through ajax, but I could not seem to achieve it. I could not download the file for some reason. What could it be that I am missing?
convert.php:
//getting the results of my query goes here
//
//
//converting the results to xls file and force downloading it goes below...
$output .= '</table>';
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=download.xls");
echo $output;
Ajax:
<script language = "javascript" type = "text/javascript">
function convert() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if(this.readyState == 4) {
xhr.close();
}
if(this.status == 200){
alert("Success!");
}
}
xhr.open("POST", "convert.php", true);
xhr.send();
}
</script>
I only get the "Success!" part working, but no file download. convert.php
is working fine if i run it normally using HTML form
alone (not through ajax). Thank you in advance for helping!