title: generate and download a .csv file with PHP and jQuery
body: <p>I want to generate a .csv file then download it with AJAX</p>
  
  <p>Insite csv.php I have this code:</p>
  
  <pre><code>&lt;?php 
  header("Content-type: text/csv");
  header("Content-Disposition: attachment; filename=file.csv");
  header("Pragma: no-cache");
  header("Expires: 0");
  
  
  $file = fopen("th.csv","r");
  $list = array();
  
  while(! feof($file))
    {
        $list[] = (fgetcsv($file));
    }
  fclose($file);
  
  $list[] = array('name5', 'town5');
  $list[] = array('name6', 'town6');
  
  $list = array_filter($list);
  outputCSV($list);
  
  function outputCSV($list) {
      $output = fopen("php://output", "w");
  
      foreach ($list as $row) {
          fputcsv($output, $row);
      }
      fclose($output);
  }
  ?&gt;
  </code></pre>
  
  <p>so when I'm going to csv.php it makes me download the csv file.</p>
  
  <p>Then inside test.php I have this jQuery code:</p>
  
  <pre><code>$(document).on('click', '#listCSV', function() {
  
          var el = $(this),
              csv = el.attr('csv');
          $.ajax({
              type: 'POST', 
              url: 'csv.php',
              data: {
                  listCSV: csv
              },
              success: function(data, textStatus, jqXHR) {
                  el.html($(data));
              }
          });
      }); 
  </code></pre>
  
  <p>But when I'm clicking on #listCSV nothing happens, nothing is being downloaded</p>
  
  <p>Any idea how can I download the csv file when clicking on #listCSV?</p>