0

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;
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Xabah
  • 41
  • 7
  • 3
    The downloaded data is going to the response callback of your $.post call, but you're not doing anything with it. Don't forget that when you do an AJAX call the response data goes back to the JavaScript, not to the rest of the browser. Check the browser's Network tool and you'll be able to see if the raw data is there in the response to the admin.php request. It is possible to then make the data download to the disk, but to be honest you might be better off forgetting about Ajax here and just doing a normal postback – ADyson Mar 16 '21 at 12:37
  • So If I understand you correctly when the download function is called, inside the switch case(php), the file goes back to the javascript? Sorry if I am not understaning it correctly but I am beginner, in web development. And maybe could you tell me how to do a "normal postback"? – Xabah Mar 16 '21 at 12:48
  • After the `readfile($file);` you **must** [die](https://www.php.net/die) or [exit](https://www.php.net/exit) the script, otherwise the rest of code will execute and append data to your downloaded file. Then you can try to fix your problem. – Binar Web Mar 16 '21 at 12:58
  • `the file goes back to the javascript`...that's correct yes. "Normal postback" is simply when you submit a form without using JavaScript or AJAX - so the browser just processes it normally and should just download the file without any extra effort – ADyson Mar 16 '21 at 13:39
  • But if you prefer to stick with the AJAX approach then see this answer for a how-to https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax – ADyson Mar 16 '21 at 13:58
  • I am not sticking to Ajax, and based on your comment I made a normal form to do it. But I have one more problem. So the file is created correctly on the server side, and the download has started, but in the downloaded CSV file, I get the full HTML code, and after that the correct data. I have added the exit() to the download function. – Xabah Mar 16 '21 at 14:18
  • Maybe there is HTML _before_ the CSV code as well? We can't really tell from what you've shown. You need to debug it – ADyson Mar 16 '21 at 14:40
  • Yes, there is html code, before it. So I sould place the function somewhere else, above the HTML code? Is it ok if I put the function in an other php file, and than call it? – Xabah Mar 16 '21 at 15:04
  • Yes you could put it in another file. Or you could just use suitable if/else control to ensure that only one type of content is output, depending on the submitted options – ADyson Mar 16 '21 at 15:43
  • What do you mean by this `only one type of content is output`? – Xabah Mar 16 '21 at 16:05
  • Either HTML content, or CSV content. Make sure it can only output one of them. You're right though - if you move the CSV-generating code into a separate PHP script then you pretty much remove any risk of a mixture of output being generated. And it provides better separation and modularity in your code as well. – ADyson Mar 16 '21 at 16:13
  • I have moved the whole function to another php file, and included it on the top, but the problem is still the same. – Xabah Mar 16 '21 at 16:52
  • Then maybe you also included something into it which outputs HTML. Hard to say exactly because we can't see the code. But please do some simple debugging and/or study your code carefully to work it out – ADyson Mar 16 '21 at 17:10
  • have you checked your browser settings? Chrome blocks multiple file downloads from the same source within a short period. Have you ever been able to download a file at all? – RisingSun Mar 16 '21 at 22:04
  • @RisingSun read the comments in full. OP is now getting a download. The current issue is its contents – ADyson Mar 16 '21 at 22:58
  • 1
    It is fully working now! I moved the function to an other PHP file, and now I am sending `$_GET` parameters to download the right file. That PHP file doesen't contain any HTML code, and it is working perfectly. Thank you! – Xabah Mar 17 '21 at 07:43

0 Answers0