1

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!

  • In order to help, we'll need to see your code for how you create the `$spreadsheet` variable, and the error message you get when you include the spreadsheet code in page-survey-to-ajax. – James Aug 24 '22 at 15:56
  • 1
    `I'm using an AJAX POST request to save the object.`...not really. The `page-survey-to-ajax.php` PHP code receives the object, echoes it out again and then discards it. Remember that web applications are _stateless_ - variables don't persist from one request to another. You'd have to explicitly save the data somewhere on the server and then retrieve it again in another script when you need it. – ADyson Aug 24 '22 at 15:56
  • @James the error is a 500 Internal Server Error. I updated the question including the snippet for generating the spreadsheet. – ilariaroglieri Aug 24 '22 at 16:03
  • @ADyson I get that, so you're suggesting the option is to save these data into a JSON file? Can you maybe point some reference to do that? I'm not skilled with these things and already tried without success... – ilariaroglieri Aug 24 '22 at 16:05
  • I think your idea is to store the data in phpspreadsheet, that should work ok. But 500 error is basically "your php broke", so finding out the error message from php would be the next step, error_reporting etc. – James Aug 24 '22 at 16:06
  • `file_put_contents("somefile.json", $json);` should be perfectly sufficient for saving the data really, so long as PHP has permissions to write to the location you choose. or you could consider using a database. What exactly did you try and what exactly went wrong? – ADyson Aug 24 '22 at 16:07
  • @ADyson I did some test and I'm able to save on the file. However the file still looks empty... Right now the only way to see the json encoded info is through the console by using `print_r($jsonD);` on `page-survey-to-ajax.php`... The very strange thing is the console shows the data if I check from other pages of the website but not from `page-survey-to-ajax.php`, where I actually send the data through AJAX... – ilariaroglieri Aug 29 '22 at 11:04

0 Answers0