How to export to csv file a PHP Array with a button?

和自甴很熟 提交于 2020-01-25 08:59:09

问题


I have 3 charts on my page, they are made from 3 php arrays. I would like to print this arrays's data by clicking a button below each chart. I could do all the queries again in the file wich will print the csv file, but I guess it would be unnecesary such repeated code this way. I tried putting the arrays data on hidden inputs, converting them with json_encode first, and then in JS by clicking these buttons will get it and send it through ajax to the file wich will convert it again to a PHP object with json_decode and then make the csv to print it. Well it won't worked and I don't know why. Here's the relevant code:

...
<canvas id="1" width='60%' height='15%'></canvas>
<input type="hidden" id="dataCanvas1" value="<?=htmlspecialchars(json_encode($valores1, JSON_UNESCAPED_SLASHES))?>">
<button type="button" class="btn btn-info" onclick="bajarCSV('dataCanvas1')">Bajar CSV</button>
...
<script>
function bajarCSV(id){
        var array = $("#"+id).val();
        $.ajax({
          data: {
            "array": array
          },
          dataType:"json",
          url:  "acciones/bajarCSV.php",
          type:  'post'
        });
      }
    </script>

Here's the "bajarCSV.php" code:

$array = json_decode($_REQUEST['array'], TRUE);

header("Content-Disposition: attachment;filename=data.csv");
header("Content-Transfer-Encoding: binary");

ob_start();
$output = fopen('php://output', 'w');
fputcsv($output, array('Fecha','Valor'));

// loop over the rows, outputting them
foreach($array as $element) {
  fputcsv($output, $element);
}

fclose($output);
return ob_get_clean();

And here's an example of how the resultant array is formed on "bajarCSV.php"

array (size=184)
  0 => 
    array (size=2)
      'x' => string '2019-10-02 11:30:00' (length=19)
      'y' => int 9266
  1 => 
    array (size=2)
      'x' => string '2019-10-02 11:33:00' (length=19)
      'y' => int 7831
  2 => 
    array (size=2)
      'x' => string '2019-10-02 11:36:00' (length=19)
      'y' => int 7649
  3 => 
    array (size=2)
      'x' => string '2019-10-02 12:57:00' (length=19)
      'y' => int 8509
  4 => 
    array (size=2)
      'x' => string '2019-10-02 13:00:00' (length=19)
      'y' => int 8815
  5 => 
    array (size=2)
      'x' => string '2019-10-02 13:03:00' (length=19)
      'y' => int 8666
  6 => 
    array (size=2)
      'x' => string '2019-10-02 11:42:00' (length=19)
      'y' => int 9064
  7 => 
    array (size=2)
      'x' => string '2019-10-02 11:45:00' (length=19)
      'y' => int 9013
...

The problem is, basically, when I click the button, the AJAX would excecute but nothing is returned from it, nor a error nor the csv printed nor the download.

I hope that all this it's well explained, thanks.

UPDATE 1

Tried the fetch aproach, but I can't/don't know how send my JSON array to the page, to form by it the csv file, so the csv resultant now it's filled with html code of the error page.

来源:https://stackoverflow.com/questions/58616572/how-to-export-to-csv-file-a-php-array-with-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!