0

I'm trying to implement excel export function for my table. I'm sending form data with ajax and php script creates file with this data and then nothing. I want ajax return the file created with php. Here is my code.

$('body').on('click', '#excel', function() {        
    var veri = $('form[method="get"]').serializeArray();
    veri['action'] = 'userexcel';
    jQuery.ajax({
           type: "POST",
           url: <?php echo '"' . admin_url( 'admin-ajax.php' ) . '"';?>,
           data: veri,
           success: function(data){
               alert(data); 
           }
        });
    return false;
});

And my php function

function exportExcel($filename='ExportExcel',$columns=array(),$data=array(),$replaceDotCol=array()){
header('Content-Encoding: UTF-8');
header('Content-Type: text/plain; charset=utf-8'); 
header("Content-disposition: attachment; filename=".$filename.".xls");
echo "\xEF\xBB\xBF"; // UTF-8 BOM

$say=count($columns);

echo '<table border="1"><tr>';
foreach($columns as $v){
    echo '<th style="background-color:#FFA500">'.trim($v).'</th>';
}
echo '</tr>';

foreach($data as $val){
    echo '<tr>';
    for($i=0; $i < $say; $i++){

        if(in_array($i,$replaceDotCol)){
            echo '<td>'.str_replace('.',',',$val[$i]).'</td>';
        }else{
            echo '<td>'.$val[$i].'</td>';
        }
    }
    echo '</tr>';
}
}
7
  • 3
    There's no need to use ajax for this. Sending proper headers for downloading will force you to stay on the same page. Commented Aug 11, 2017 at 12:01
  • This is a wordpress site so it is better to use wp's ajax functions for security reasons. Commented Aug 11, 2017 at 12:04
  • 1
    security reasons?? Either way it is a http request... lol Commented Aug 11, 2017 at 12:08
  • What are the reasons? Someone will find the uri? Commented Aug 11, 2017 at 12:08
  • Wordpress has "nonce" thing for requests and it is very powerful i think. I dont want to mess with securing my script while wordpress does most things for me. Like checking user logged in, privileges etc. Commented Aug 11, 2017 at 12:12

1 Answer 1

1

The generation code works just fine, however, if you want to force the browser to download that file, you have two options.

Create a local file

You'll have to actually save that file somewhere on the server, then return the link to it and then initiate the download by changing the window.location.href = "<url_of_xls_file>" after the successful AJAX call.

Make the form post directly to the AJAX call

Now, if you don't want to create an file on the server, then you'll have to get rid of the approach, change your form action attribute to admin_url( 'admin-ajax.php' ) and manually add the <input type="hidden" name="action" value="userexcel"> action inside your form.

You'll have the browser to popup the download dialog with both of these methods.

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

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.