2

Question:: 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?

Where I am?: I have created the hook and the handler. I can actually grab the data. Please see my code below.

add_action ( 'wp_ajax_nopriv_exportcsv', 'exprot_to_csv' );
add_action ( 'wp_ajax_exportcsv', 'exprot_to_csv' );    

<script type="text/javascript">
 jQuery("#export-csv").on("click",function(e){
        jQuery.ajax({
            type: "POST",
            url: ajaxurl,
            data: {"action":"exportcsv"},
            success: function(response){alert(response);}
            })
        });</script>        


function exprot_to_csv(){

global $wpdb;
$table_prefix = $wpdb->prefix;
$table = $table_prefix.'my_custom_table';

$result = $wpdb->get_results("SELECT * FROM ".$table."");
    foreach ($result as $row) {

        $output .="\n";
        $output .='"'.$row->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;

}

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.

Any help?

12
  • Can you check with your FTP to see if in the root of the wp folder you have a .csv file? Commented Mar 22, 2016 at 11:17
  • Just checked. Its not in the server. Commented Mar 22, 2016 at 11:19
  • 1
    Possible duplicate of Download CSV file using "AJAX" Commented Mar 22, 2016 at 11:21
  • @dingo_d, I did have checked that. But he is getting a file created in the server but can't show the download dialogue box. While my file isn't actually being created. Commented Mar 22, 2016 at 11:29
  • How not? Just put the contents of your function exprot_to_csv() to exprot_to_csv.php, and call that file on click ;) Should be working :) Commented Mar 22, 2016 at 11:30

2 Answers 2

2

You are close...! I ran into same problem, tried a similar solution.

Check this question out: Download file through an ajax call php

And THIS specific answer: https://stackoverflow.com/a/11452917 Make sure to look at Dario's answer

Summary -

  1. AJAX is not for streaming downloads.
  2. The solution here is to make an iFrame
  3. Load a PHP file into that iFrame using jQuery
  4. Serve up your file with a File Transfer

    'header('Content-Description: File Transfer');'
    

This works in WordPress. Use Admin-Ajax. Cheers

Sign up to request clarification or add additional context in comments.

Comments

0

Hopefully this will solve your problem.

In jQuery:

 window.location = response->in success

Action ajax:

Please remove all headers

echo admin_url($filename);
die;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.