I am exporting a excel sheet using php script and my code is
<?php
function excel_file(){
header("Content-Disposition: attachment; filename=\"demo.xls\"");
header("Content-Type: application/vnd.ms-excel;");
header("Pragma: no-cache");
header("Expires: 0");
$out = fopen("php://output", "w");
foreach ($tabledata as $data)
{
fputcsv($out, $data,"\t");
}
fclose($out);
}
?>
HTML
<input type="button" name="excel_file" onclick="<?php excel_file(); ?>" value="Download File">
Problem is this:
This
function excel_file()execute on loading the page not on click.The excel file which i get, includes the data of whole page not the data of that perticular array "
$tabledata"
$tabledataisn't available in the scope of yourexcel_file()function, and you need to suppress outputting your html markup if you're outputting the CSV