csvexport

Wordpress Custom Post type csv exporter


I'm trying to export custom post-type data csv format with Ajax, but it exporting a blank csv file, but when I see it on the js console it shows all data like the screenshot enter image description here

Please help me to figure out the issues

Here is the PHP code, it just pull a custom posttype data with ajax all in the modal

 public function export_result_entry_csv(){


  $data = $_POST['data'];
  $args = [
   'post_type' => 'student',
   'posts_per_page'=> -1,
   'meta_key' => 'student_roll_number',
   'orderby' => 'meta_value_num',
   'order' => 'ASC',
   'tax_query' => [
       [
           'taxonomy' => 'year',
           'terms' => $data['year'],
           'field' => 'slug',
           'operator' => 'IN'
           ]
   ],
   'meta_query'    => [
       'relation' => 'AND',
       [
           'key'       => 'wp_sr_student_main_subject',
           'value'     => $data['subject'],
           'compare'   => 'LIKE',
       ],
       [
           'key'       => 'wp_sr_student_class',
           'value'     => $data['class'],
           'compare'   => 'LIKE',
       ]
   ]

];
$query = new \WP_Query($args);  
$delimiter = ","; 
$filename = "members-data_" . date('Y-m-d') . ".csv"; 
$f = fopen('php://memory', 'w'); 
$fields = array('ID', 'FIRST NAME', 'CREATED'); 
fputcsv($f, $fields, $delimiter); 

while($query->have_posts())  : $query->the_post();
$lineData = array(get_the_ID(), get_the_title(), get_the_time()); 
fputcsv($f, $lineData, $delimiter); 

endwhile;

fseek($f, 0); 
     
// Set headers to download file rather than displayed 
header('Content-Type: text/csv'); 
header('Content-Disposition: attachment; filename="' . $filename . '";'); 
 
//output all remaining data on a file pointer 
fpassthru($f); 

   wp_die();
 }

Js Code for this

jQuery(function($){
    "use strict"
    $(document).on('click', '.esrp-export-entry', function(e){
        e.preventDefault();
        var data = {
            'action': 'export_result_entry_csv',
            '_wpnonce': esrp_data._nonce,
            'data' : $(this).parents('.show-mark-entry-form').siblings('.wsr-result-generate').data('info')
        };

        // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
        jQuery.post(esrp_data.ajaxurl, data, function(response) {
            let url = ajaxurl+'?action=export_result_entry_csv';
             console.log(url);
             let a = document.createElement('a');
                a.href = url;
                a.download = 'export.csv';
                document.body.appendChild(a);
                a.click();
                a.remove();
          
           console.log(response)
        
           //display the created CSV data on the web browser 
           $('.downlaod').html(response);
        });
    });
});  

Where is the wrong ??? Can anyone please help me???


Solution

  • I was trying to get value with the POST request, but it should be GET request