views:

43

answers:

1

UPDATE:

Solved via:

            append = append+'/output/'+output;
            var url = '/producer/json/index/period/'+period+'/empties/'+empties+'/movies/'+movies+'/fields/'+fields+'/start/'+start+'/end/'+end+append+'';
            $.ajax({ 
                url: ''+url+'',  
                success: function(msg) {
                    location.href = ''+url+'';
                }
            });

Hello,

I have a piece of Javascript that pings a URL and updates a table. I now want to output a CSV file of that.

So my PHP side is done, the file is created and output correctly if I view the URL the ajax pings.

However, how do I get jQuery/Javascript to output the content its retrieved as a download?

PHP:

public function outputCSV($main, $fields) {

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/excel');
    header('Content-Disposition: attachment; filename="ProducerAreaOutput-'.date('r').'.csv"');

    $array      = array();//make a new array, put the fields first
    $array[0]   = $fields;

    foreach ($main as $item) {//loop existing rows
        $array[] = $item;
    }

    $csv = self::array_to_csv($array, false);

    echo $csv;
    exit;
}

The Javascript

    function byMonth(output) {

        var undefined;
        oTable = $('#stats').dataTable();
        oTable.fnClearTable();

        var start       = $('#year-start').val()+'-'+$('#month-start').val()+'-1';
        var end         = $('#year-end').val()+'-'+$('#month-end').val()+'-30';
        var empties     = $('#empties').val();
        var period      = $('#period').val();
        var movies      = $('#movies').val();
        var site_id     = $('#site_id').val();
        var studio_id   = $('#studio_id').val();
        var movie_id    = $('#movie_id').val();
        var append      = '';


        append = (site_id > 0)      ? append+'/site_id/'+site_id        : append;
        append = (studio_id > 0)    ? append+'/studio_id/'+studio_id    : append;
        append = (movie_id > 0)     ? append+'/movie_id/'+movie_id      : append;


        if (output === 'CSV') {
            append = append+'/output/'+output;
            $.ajax({ url: '/producer/json/index/period/'+period+'/empties/'+empties+'/movies/'+movies+'/fields/'+fields+'/start/'+start+'/end/'+end+append+''});
        } 

Ideas?

+1  A: 

Hi, I don't think that is possible because all the headers are already sent. Do as Pointy says, divide this into two parts, one which answer the ping the the one which delivers the file.

Bandpay