0

I have 3 charts on my page, they are made from 3 PHP arrays. I would like to print the data from these arrays by clicking a button below each chart. I could do all the queries again in the file which will print the csv file, but I guess it would be unnecessary to have 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 which will convert it again to a PHP object with json_decode and then make the csv to print it. Well it didn't work 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 execute 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 approach, 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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
DiegoB
  • 57
  • 1
  • 8
  • 1. At least for your sample, you don't need php for it. 2. You are doing nothing with the ajax response. Try searching: how to download ajax file. – Elias Soares Oct 30 '19 at 00:03
  • Possible duplicate of [Download a file by jQuery.Ajax](https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax) – Elias Soares Oct 30 '19 at 00:04
  • For a PHP-less solution, see this question: https://stackoverflow.com/q/14964035/3429323 – Elias Soares Oct 30 '19 at 00:06

0 Answers0