ajax - How to export custom database data to excel file

admin2025-01-07  6

Hi everyone I have the code below, but when I click on the link generated a new page is open with 0 display on it. What I'm doing wrong?

function list_reservation_to_csv() {

 global $wpdb;
 $table_name = $wpdb->prefix . 'prenotazione_eventi';
 $file = 'email_csv'; // ?? not defined in original code
 $results = $wpdb->get_results("SELECT * FROM $table_name",ARRAY_A);

 if (empty($results)) {
   return;
 }

 $csv_output = '"'.implode('";"',array_keys($results[0])).'";'."\n";;

 foreach ($results as $row) {
  $csv_output .= '"'.implode('";"',$row).'";'."\n";
 }
 $csv_output .= "\n";

 $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");
 print $csv_output;
 exit;

 }
add_action('wp_ajax_csv_pull','list_reservation_to_csv');

$ajax_url = admin_url('admin-ajax.php?action=csv_pull');

<a href="<?php echo $ajax_url; ?>"><button>Scarica csv</button></a>

Hi everyone I have the code below, but when I click on the link generated a new page is open with 0 display on it. What I'm doing wrong?

function list_reservation_to_csv() {

 global $wpdb;
 $table_name = $wpdb->prefix . 'prenotazione_eventi';
 $file = 'email_csv'; // ?? not defined in original code
 $results = $wpdb->get_results("SELECT * FROM $table_name",ARRAY_A);

 if (empty($results)) {
   return;
 }

 $csv_output = '"'.implode('";"',array_keys($results[0])).'";'."\n";;

 foreach ($results as $row) {
  $csv_output .= '"'.implode('";"',$row).'";'."\n";
 }
 $csv_output .= "\n";

 $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");
 print $csv_output;
 exit;

 }
add_action('wp_ajax_csv_pull','list_reservation_to_csv');

$ajax_url = admin_url('admin-ajax.php?action=csv_pull');

<a href="<?php echo $ajax_url; ?>"><button>Scarica csv</button></a>
Share Improve this question asked Apr 26, 2018 at 12:10 Francesco TontiFrancesco Tonti 111 silver badge2 bronze badges 3
  • Are you displaying PHP errors? If this is the literal source code, it's bound not to work because PHP and HTML code are intermixed. Turn error reporting on; without it all people can do is guess. – Pekka Commented Apr 26, 2018 at 12:38
  • nope, no errors. Now I'm thinking... wp_ajax_csv_pull action works only in frontend or also in backend? – Francesco Tonti Commented Apr 26, 2018 at 12:47
  • If the code is literally like it is above, there is bound to be a syntax error on the last line though. – Pekka Commented Apr 26, 2018 at 12:54
Add a comment  | 

1 Answer 1

Reset to default 0

Try adding a nopriv hook

add_action('wp_ajax_nopriv_csv_pull','list_reservation_to_csv');

This adds the option to run the function without being logged in as a registered user.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257352a431.html

最新回复(0)