I have a problem with phpspreadsheet.
I need to pass data to a php file generating an excel file.
I can do it with a
window.location.href='pagegeneratingexcel.php?parameter=parvalue';
In the pagegenerating excel I can easily retrieve the GET variable.
But I need to pass a number of variables, or arrays, then I should use a POST, instead.
I tried with ajax
postData = { parameter: "parvalue"};
$.ajax({
url : "pagegeneratingexcel.php",
type: "POST",
data : postData,
dataType: "json",
success:function(data){
console.log(data);
}
})
but this is giving a 500 error.
I tried by generating a dynamic form and submitting it...something like:
let form = $(document.createElement('form'));
$(form).attr("action", "pagegeneratingexcel.php");
$(form).attr("method", "POST");
$(form).css("display", "none");
let input1 = $("<input>")
.attr("type", "text")
.attr("name", "parameter")
.val("parvalue");
$(form).append($(input1));
form.appendTo( document.body );
$(form).submit();
still no way: it gives a 500 error.
The problem seems to be linked to this line in the pagegeneratingexcel.php file:
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("TemplateExport.xlsx");
This is where it stops: this instruction (required by my excel file generation, though) seems to be in conflict with a POST of any type while everything works perfectly with a GET:
window.location.href='pagegeneratingexcel.php?parameter=parvalue';
or - when there is no data passed:
window.location.href='pagegeneratingexcel.php';
Just for info the pagegeneratingexcel.php is something like
require_once('vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("TemplateExport.xlsx");
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->SetCellValue("A1", "MYVALUES");
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="OUTPUTFILE.xlsx"');
$writer->save('php://output');
My Answer:
parvalue };
$.ajax({
type: 'POST',
url: 'pagegeneratingexcel.php',
data: postData,
success: function(response) {
console.log(response);
}
});
__But in the pagegeneratingexcel.php I cannot retrieve the POST variable._How can I solve this issue?
Rate this post
5 of 5 based on 7745 votesComments