0

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!

GenesisTepait
  • 63
  • 1
  • 10
  • The point of Ajax is that the responses is *handled by JavaScript* – Quentin Jul 23 '18 at 09:50
  • Store post data into session in first AJAX request then use window.open("https://domain-name/convert.php"); inside AJAX response. and destroyed the POST session after the file download code execution – Rajinder Jul 23 '18 at 09:53

0 Answers0