i'm trying to export my plugin table in the same page. i tried to do hidden iframe but it still does not work
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=1&filename=" data '.csv';
}
CodePudding user response:
Instead of echoing the filename, echo the file contents.
<?php
echo $csv_output;
exit;
An alternative is to not use Ajax. Simply navigate to the Ajax URL via a link
<a href="<?php echo admin_url( 'admin-ajax.php' ); ?>?action=myexport">Export</a>
As the file is force downloaded the the url of the browser will remain the same.