2
I’m making a link, that when the user clicks on it should generate an xls file. The link has the data-attributes and must pass these attributes to the.php page and generate the xls. I tried to do with jquery and it’s not working, is there some other way?
<a href="#" id="xls" data-unidade="203" data-tema="1">
Gerar XLS
</a>
The jquery
$(document).ready(function() {
$('#xls').click(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url : 'produto/exportXls',
data: {
'tema_id' : $(this).data('tema'),
'unidade_id' : $(this).data('unidade')
}
});
});
});
php that creates the table to generate the xls
$html = '<table><tr>';
$html .= '<td colspan="3">Planilha teste</tr>';
$html .= '</tr>';
$html .= '<tr><td><b>Coluna 1</b></td><td><b>Coluna 2</b></td><td><b>Coluna 3</b></td></tr>';
$html .= '<tr><td>L1C1</td><td>L1C2</td><td>L1C3</td></tr>';
$html .= '<tr><td>L2C1</td><td>L2C2</td><td>L2C3</td></tr>';
$html .= '<tr><td>L3C1</td><td>L3C2</td><td>L3C3</td></tr>';
$html .= '</table>';
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/x-msexcel");
header("Content-Disposition: attachment; filename=arquivo.xls" );
header("Content-Description: PHP Generated Data" );
// Envia o conteúdo do arquivo
echo $html;
Only it’s not happening...
The html is displayed on the screen this is the problem?
– rray
actually does not display on the screen, only in firebub->Network->preview. And when I run the direct url it generates the xls file.
– Marcelo Diniz
What’s the mistake? What was supposed to happen?
– Guilherme Oderdenge
"Only it’s not happening..." is very vague. First isolate the problem: Does the application work without AJAX? If it works, the problem is in Javascript. If it doesn’t work, it’s in PHP. Second, in your AJAX, where is the callback success? Without it you can’t do anything with the server response. Third: Do you really need AJAX for that? Why not pass the arguments via querystring in a link once the file will be downloaded?
– Bruno Augusto
@Guilhermeoderdenge was to download the file.
– Marcelo Diniz
@Brunoaugusto great questions you raised... I will test here and warning. I was wondering that clicking on the link will redirect you to the link page. But I will take the test.
– Marcelo Diniz