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>';
}
}