WordPress Export table as csv using Ajax
Hello guys i'm trying to export my plugin table in the same page, i tried to do hidden iframe but it's still not works, can you kindly check my code please? thanks.
PHP:
add_action( wp_ajax_export_plugin, export_plugin ); //update_records is action
add_action( wp_ajax_nopriv_export_plugin, export_plugin );
function export_plugin(){
global $wpdb;
$wpdb-show_errors = TRUE;
$wpdb-suppress_errors = FALSE;
$plugin_name = $_POST['plugin-name']; // PLUGIN NAME
$plugin_export_type = $_POST['export-type']; // PLUGIN EXPORT TYPE as CVS/SQL/XML
$plugin_name; // ?? not defined in original code
$results = $wpdb-get_results(SELECT * FROM `wp_myplugin`,ARRAY_A);
$csv_output = ''.implode(';',array_keys($results[0])).';'.\n;;
foreach ($results as $row) {
$csv_output .= ''.implode(';',$row).';'.\n;
}
$csv_output .= \n;
$filename = $plugin_name._.date(Y-m-d_H-i,time());
header(Content-type: application/force-download);
header(Content-disposition: csv . date(Y-m-d) . $plugin_export_type);
header( Content-disposition: filename=.$plugin_name.$plugin_export_type);
header('Content-Description: File Transfer');
echo $filename;
exit;
}
AJAX JQUERY:
success:function(data){
$('#ifram_download').src=/app?download=1filename= + data + '.csv';
}