title: Pass data from js to php and download created csv file
body: <p>I must pass array from JS to PHP using $.post(), create file using array and download it.</p>
  
  <p>Using this pass array:</p>
  
  <pre><code>$('#csv').click(function () {
  $.post(
      window.location + "crawler/save_to_file",
      {
          dane: wynik //array
      });
    });
  </code></pre>
  
  <p>Now in PHP using this:</p>
  
  <pre><code>$tablica=$_POST['dane'];
   $filename = "export-to-csv.csv";
                  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                  header("Content-type: text/csv");
                  header("Content-Disposition: attachment; filename=\"$filename\"");
                  header("Expires: 0");
                  $fh = fopen( 'php://output', 'w' );
                  $heading = false;
                  if(!empty($tablica))
                      foreach($tablica as $row) {
                          if(!$heading) {
                              fputcsv($fh, array_keys($row));
                              $heading = true;
                          }
                          fputcsv($fh, array_values($row));
                }
                  fclose($fh);
  </code></pre>
  
  <p>But when click on button to create and download file, nothing happens.</p>
  
  <p>Thanks</p>
  
  <p><strong><em>CODE UPDATE</em></strong></p>
  
  <p>JS file:</p>
  
  <pre><code>   $.ajax({
          url: window.location + "crawler/",
          type: "POST",
          dataType: "json",
          data: {
              wartosc: zmienna
          },
          success: function (odp) {
              wynik = odp;  //array
              tab = JSON.stringify(odp);
              $.post(window.location + "crawler/return_data",
                  {
                      data: tab
                  },
                  function (data) {
                      $('#wynik').html(data);
                      $('.pobierz').show();
                  }
              )
          }
      })
  
  
  $('.button').click(function() {
      var $form = $('&lt;form action="' + window.location + 'crawler/save_to_csv" method="post"&gt;&lt;/form&gt;');
      $.each(wynik, function() {
          $('&lt;input type="hidden" name="dane[]"&gt;').attr('value', this).appendTo($form);
      });
      $form.appendTo('body').submit();
  });
  </code></pre>
  
  <p>And var_dump array in PHP file:</p>
  
  <pre><code>function save_to_csv()
  {
      $tablica=$_GET['dane'];
      var_dump($tablica);
  
  }
  </code></pre>
  
  <p>return: "Undefined index: dane"</p>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>$tablica must be <code>$_POST</code> not <code>$_GET</code></p>