title: Create CSV File by php, ajax and mysql
body: <p>I have the following problem:</p>
  
  <p>I'm upgrading a working system, so I have to create code that fits in.
  I want to create a csv file on a php page and a mysql database. I'm also using Ajax to stay on the same page while running other php-Files. Here are the Code snippets:</p>
  
  <p>PHP/HTML-Page with Button</p>
  
  <pre><code>&lt;div class="btn" id="export"&gt;Export&lt;/div&gt;
  </code></pre>
  
  <p>Javascript Ajax</p>
  
  <pre><code>$("#export").click(function() {exportInfos();});
  
  function exportInfos() {
      $.ajax({
          type: "POST",
          url: "functions/exportInfos.php",
          data: { searchterm: $("#search").val(), filterbycat: $("#filterbycat").val(), filterbytype: $("#filterbytype").val()},
          success: function(response){
              console.log("success_export");
          },
      });
  }
  </code></pre>
  
  <p>PHP-File to create csv(exportInfos.php):</p>
  
  <pre><code>&lt;?php
  include ('../../config.php');
  
  $searchterm = $_POST["searchterm"];
  $filterbycat = $_POST["filterbycat"];
  $filterbytype = $_POST["filterbytype"];
  
      header('Content-Type: text/csv');
      header('Content-Disposition: attachment; filename=export.csv');
      $output = fopen("php://output", "w");
          $query = "SELECT * FROM database";
          $result = mysqli_query($mysqli, $query);
          while($row = mysqli_fetch_assoc($result))
          {
                   fputcsv($output, $row);
          }
          fclose($output);
  ?&gt;
  </code></pre>
  
  <p>I'm not sure where the Problem is but in the console I only see that the php script is called and the success_export text from the log, but no file is opened or downloadable. I think the problem could be with the AJAX part because thats the part Im not sure about the most. </p>
  
  <p>The data values in the Ajax part are there to edit the query as soon as i get some output File.
  $mysqli is the connection defined in the config file.</p>