0
good afternoon I am passing via ajax some fields of my modal to a PHP function. In this way, I run the query below in the PHP function to generate an Excel spreadsheet. the problem is that: I even receive the values via POST in PHP but gives error in the creation of EXCEL spreadsheet. If I put the fixed value in the variable, the worksheet is created without problems. Where I’m going wrong on this case ?
function exp_excel()
{
var url = "<?php echo site_url('ExpExcel/exportExcel')?>";
$.ajax({
url : url,
type: "POST",
data: $('#form_1').serialize(),
dataType: "HTML",
success: function(data)
{
window.location = url;
$('#modal_excel').modal('hide');
},
error: function (request, status, erro) {
},
});
}
public function exportExcel()
{
//$gestor = $_POST["NomeGestor"]; -> assim nao funciona
//$qtdDias = $_POST["qtdDias"];
$gestor = "Francisco Jose"; -> assim funciona normal
$qtdDias = "3";
try{
$query = "SELECT * FROM tbl_dpo_1 WHERE
gestor LIKE '{$gestor}' AND (data_oper >= DATE_SUB(CURDATE() ,INTERVAL {$qtdDias} DAY)
AND data_oper <= CURRENT_DATE()) AND (operacao LIKE 'UPD_REG' OR operacao LIKE 'NEW_REG')";
$resultado = mysql_query($query);
//CRIA O OBJETO PARA EXPORTAR PARA O EXCEL
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$data = date('dmy');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="DPO-Consolidado_'. $data .".xlsx");
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
}
Bruno, the indication "Manager name" is in the "name" field of the form?
– Rodrigo Tognin
A suggestion would also be to use lower-case variable names. Another thing I saw to be different from what I did was the indication of dataType. I put "html" (all in lower case), I don’t know if it would affect anything.
– Rodrigo Tognin
This is Rodrigo, I’m switching to minuscule. this field is in the modal: <select class="form-control" name="Manager name" id="Manager name"> and I will also change the html to minuscule.
– Bruno
I changed there but it didn’t solve no. Passing the variables, it even triggers the download of the spreadsheet, but gives the error: "Excel cannot open the file 'DPO-Consolidated' 110718.xlsx. why the format or file extension is not valid." and if I leave the fixed value, it downloads the spreadsheet normally with the records.
– Bruno
Bruno, put inside the exportaExcel function of PHP a var_dump($_POST); to force display what it is receiving from Ajax... Is the error when receiving the data from the Post and when mounting Select, it does not find the name in the database? One more thing, try to exchange the LIKE for = when making the direct comparison.
– Rodrigo Tognin
Rodrigo, the dump came out like this: <pre class='Xdebug-var-dump' dir='Ltr'> <b>array</b> <i>(size=2)</i> 'manager name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'Francisco Jose'</font> <i>(length=15)</i> 'qtdDias' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'3'</font> <i>(length=1)</i>. I did a test with = in query but gives the same problem. in my function I am passing the post like this: $manager = $_POST["manager name_name"]; $qtdDias = $_POST["qtdDias"];
– Bruno
A little strength there guys? I need to solve it. Thank you very much.
– Bruno
Bruno, this is challenging. I suspect that something is wrong when setting up Select with the data coming from the POST. Maybe you are missing quotes, or you have too many quotes... do a var_dump($query); to see how he is mounting Select with the data coming from the POST to see.
– Rodrigo Tognin
You’ve already solved?
– White
I got nothing White. I’ve tried everything and it’s not going forward
– Bruno
Rodrigo is very strange, because when I trigger the ajax method, in return, he brings me the select in the dump and more the values I need the query. but soon after, PHP error occurs saying that the fields I am passing are undefined.
– Bruno
Would anyone have any idea what might be wrong ?
– Bruno