Skip to content

Instantly share code, notes, and snippets.

@kankaungmalay
Created December 5, 2012 09:01
Show Gist options
  • Save kankaungmalay/4214031 to your computer and use it in GitHub Desktop.
Save kankaungmalay/4214031 to your computer and use it in GitHub Desktop.
Html form with hidden data called csv_hdr and csv_output.
/***********************************************************/
$(document).ready(function(){
$("#btn_challenge_score_details").click(function (){
var csv_hdr = $("#csv_hdr").val();
var csv_output = $("#csv_output").val();
var dataString = 'csv_hdr='+ csv_hdr + '&csv_output=' + csv_output;
//alert (dataString); return false;
$.ajax({
type: "POST",
url: "<?=base_url()?>challenge/export_csv",
data: dataString,
cache:false,
success:
function(data){
$("#message").html(data);
}
});
return false;
});
});
/***********************************************************/
Controller method
function export_csv()
{
$out = '';
if (isset($_POST['csv_hdr']))
{
$out .= $_POST['csv_hdr'];
$out .= "\n";
}
if (isset($_POST['csv_output']))
{
$out .= $_POST['csv_output'];
}
$file = 'ExportCSV';
$filename = $file."_".date("Y-m-d_H-i",time());
//Generate the CSV file header
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: attachment; filename=".$filename.".csv");
print $out;
exit;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment