title: WordPress - Export table as csv using jQuery/Ajax
body: <p><strong>Question:</strong>: I am developing a plugin. This plugins creates a custom table in WP database. How can I export the data from this table using Ajax?</p>
  
  <p><strong>Where I am?</strong>: I have created the hook and the handler. I can actually grab the data. Please see my code below.</p>
  
  <pre><code>add_action ( 'wp_ajax_nopriv_exportcsv', 'exprot_to_csv' );
  add_action ( 'wp_ajax_exportcsv', 'exprot_to_csv' );    
  
  &lt;script type="text/javascript"&gt;
   jQuery("#export-csv").on("click",function(e){
          jQuery.ajax({
              type: "POST",
              url: ajaxurl,
              data: {"action":"exportcsv"},
              success: function(response){alert(response);}
              })
          });&lt;/script&gt;        
  
  
  function exprot_to_csv(){
  
  global $wpdb;
  $table_prefix = $wpdb-&gt;prefix;
  $table = $table_prefix.'my_custom_table';
  
  $result = $wpdb-&gt;get_results("SELECT * FROM ".$table."");
      foreach ($result as $row) {
  
          $output .="\n";
          $output .='"'.$row-&gt;post_id.'",';
          }
          $output .="\n";
          $file = "custom_table";
          $filename = $file."_".date("Y-m-d_H-i",time());
          header("Content-type: application/vnd.ms-excel");
          header("Content-disposition: csv" . date("Y-m-d") . ".csv");
          header( "Content-disposition: filename=".$filename.".csv");
  
          echo $output;
          exit;
  
  }
  </code></pre>
  
  <p>In the alert box, I am getting comma separated post IDs as expected. But I am not sure how to move on from here. I mean how to handle this response and prompt the user to save/download the file. </p>
  
  <p>Any help?</p>