I'm struggling with this for a while and need help.
I already tried all solutions from Receive JSON POST with PHP without luck.
Basically, I have a Javascript object saved in Localstorage where user inputs from a survey are saved. I need to retrieve these values in PHP in order to save them into a downloadable Excel file using Phpspreadsheet.
I'm using an AJAX POST request to save the object.
$.ajax({
url: '../wp-content/themes/elsa_theme/page-survey-to-ajax.php',
type: "POST",
dataType: "text",
data: parsedSurvey,
success: function(data) {
console.log(data); // this outputs the object in the console successfully
}
});
This is page-survey-to-ajax.php
code:
<?php
if (isset($_POST)) {
$json = json_encode($_POST);
$jsonD = json_decode($json, true);
print_r($jsonD);
} else {
echo 'nothing to show';
}
?>
The request works successfully as I see the object in the console (outputted through the success function inside AJAX).
Array
(
[transport] => Array
(
[bensin] => Array
(
[name] => bensin
[index] => 1
[excelSheet] => Transport
[keyword] => Bensin
[totEmissions] => 220
[parameters] => Array
(
[0] => Array
(
[excelCell] => G8
[coefficient] => 0.32
[percent] => false
[value] => 200
[emissions] => 64
)
[1] => Array
(
[excelCell] => G14
[coefficient] => 0.39
[percent] => false
[value] => 400
[emissions] => 156
)
)
)
[el] => Array
(
[name] => el
[index] => 4
[excelSheet] => Transport
[keyword] => El
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => G11
[coefficient] => 0.08
[percent] => false
[value] =>
[emissions] => 0
)
[1] => Array
(
[excelCell] => G17
[coefficient] => 0.12
[percent] => false
[value] =>
[emissions] => 0
)
)
)
[hemleverans] => Array
(
[name] => hemleverans
[index] => 5
[excelSheet] => Transport
[keyword] => Hemleverans från butik (ej bud)
[totEmissions] => 654
[parameters] => Array
(
[0] => Array
(
[excelCell] => G26
[coefficient] => 3.27
[percent] => false
[value] => 200
[emissions] => 654
)
)
)
[Locationvan/Platsbil] => Array
(
[name] => Locationvan/Platsbil
[index] => 6
[excelSheet] => Transport
[keyword] => Locationvan/Platsbil
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => G29
[coefficient] => 0.48
[percent] => false
[value] =>
[emissions] => 0
)
)
)
[saxkran] => Array
(
[name] => saxkran
[index] => 7
[excelSheet] => Transport
[keyword] => Saxkran
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => G30
[coefficient] => 0.48
[percent] => false
[value] =>
[emissions] => 0
)
)
)
[buss] => Array
(
[name] => buss
[index] => 8
[excelSheet] => Transport
[keyword] => Buss
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => G33
[coefficient] => 0.05
[percent] => false
[value] =>
[emissions] => 0
)
)
)
[flyg-inrikes] => Array
(
[name] => flyg-inrikes
[index] => 9
[excelSheet] => Transport
[keyword] => Flyg (inrikes)
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => G37
[coefficient] => 0.22
[percent] => false
[value] =>
[emissions] => 0
)
)
)
[flyg-utrikes] => Array
(
[name] => flyg-utrikes
[index] => 10
[excelSheet] => Transport
[keyword] => Flyg (utrikes)
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => G38
[coefficient] => 0.17
[percent] => false
[value] =>
[emissions] => 0
)
)
)
)
[lokaler] => Array
(
[studio] => Array
(
[name] => studio
[index] => 11
[excelSheet] => Lokaler och boende
[keyword] => studio
[totEmissions] => 1000
[parameters] => Array
(
[0] => Array
(
[excelCell] => F8
[coefficient] => 5
[percent] => false
[value] => 200
[emissions] => 1000
)
)
)
[hyrd] => Array
(
[name] => hyrd
[index] => 15
[excelSheet] => Lokaler och boende
[keyword] => hyrd
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => E15
[coefficient] => 1
[percent] => false
[value] =>
[emissions] => 0
)
[1] => Array
(
[excelCell] => F15
[coefficient] => 0.02
[percent] => false
[value] =>
[emissions] => 0
)
)
)
[vandrarhem] => Array
(
[name] => vandrarhem
[index] => 18
[excelSheet] => Lokaler och boende
[keyword] => Vandrarhem
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => F27
[coefficient] => 0.62
[percent] => false
[value] =>
[emissions] => 0
)
)
)
[frigolitskivor] => Array
(
[name] => frigolitskivor
[index] => 20
[excelSheet] => Övrigt
[keyword] => Frigolitskivor
[totEmissions] => 0
[parameters] => Array
(
[0] => Array
(
[excelCell] => F12
[coefficient] => 3.1
[percent] => false
[value] =>
[emissions] => 0
)
)
)
)
)
However I have no clue on how to access these data and use them.
If I look at page-survey-to-ajax.php
in the browser I see an empty array.
I need to access it back in order to use Phpspreadsheet to save the data inside an Excel file.
This is the snippet to access the excel file:
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
$template = get_stylesheet_directory_uri() . '/assets/php/elsa_report.xlsx';
$file = file_get_contents($template);
$inputFileName = 'tempfile.xlsx';
file_put_contents($inputFileName, $file);
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
I also already wrote the part to fetch the right data, it works correctly inside page-survey-to-ajax.php
but when I insert Phpspreadsheet code everything breaks...
Therefore no idea of where to put it.
foreach($jsonD as $cats){
foreach($cats as $question){
$sheet = $question['excelSheet'];
$tot = $question['totEmissions'];
$parameters = $question['parameters'];
if (intval($tot) != 0) {
foreach($parameters as $parameter){
$cell = $parameter['excelCell'];
$value = $parameter['value'];
// $spreadsheet->getSheetByName($sheet)->getCell($cell)->setValue($value);
}
}
}
}
Any help would be hugely appreciated!