0

In my application i store my date in a localStorage variable called "postResult"

localStorage.setItem("postResult", JSON.stringify(result));

In next step i store the data in a PHP variable.

$posts = "<script>document.write(localStorage.getItem('easyscanResult'))</script>";

When i output the result i can see a object string. Here is a small excerpt of my data:

{"posts":"20","findings":[{"title":"test title","category":"healthy","data":{"id":314,"date":"2023-02-21T10:53:01.268Z","slug":"test-title","name":"Test title","description":"Lorem ipsum dolor sit amet consectetur adipisicing elit. Eveniet sequi at cupiditate delectus quis commodi reiciendis obcaecati a eligendi, unde consequuntur? Repellat eos accusantium voluptatum expedita cumque doloribus dolorem libero?

I want to translate the data in an PHP Object

<?php $posts = json_decode($posts); ?>

But now the result is null and i have no idea why?

I tried the string already in an Online Converter. I have no problems so the syntax is fine.

What have i'm forgotten?

Edit: The $posts returns a string not an object. I want to translate the string into a PHP object. This not work for me.

  • 1
    *"In next step i store the data in a PHP variable."* That isn't what that's doing. Local storage is **client-side**. Your PHP code runs **server-side**. If you want to send the client-side information to the server, you have to do that via form submission, ajax, or similar. – T.J. Crowder May 03 '23 at 08:30
  • Related: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – T.J. Crowder May 03 '23 at 08:31
  • But it work for me and i can the see output of the $variable. Not work is when i wrap the $value in the function json_decode($posts). Then it returns null. It's not a problem to pass data from JS to PHP. –  May 03 '23 at 08:50
  • 1
    `$posts` - as per your code - is a string which contains a ` – ADyson May 03 '23 at 08:50
  • You seem to be expecting that the moment you write `$posts = ...` that JS code will be executed and `$posts` will somehow then contain the output of executing that JS. But that's not how it works at all...PHP runs on the server. JS runs in the browser. Those are two totally separate contexts. The JS doesn't run until the PHP script has finished, and the _output_ of the PHP script has been sent to the browser. They are not done in parallel. Seriously, read the link TJ Crowder provided, to get the concepts clear in your mind. – ADyson May 03 '23 at 08:52
  • If you stored some value in localStorage in the browser, and you want to process that value in some PHP code, then you must put into your browser the JS code which can retrieve that value from storage, and then you must make a HTTP request to your server to trigger a PHP script, and send that stored value as data in that request, so that PHP can receive and process it. Because at the moment, in the browser, when you retrieve the stored value, there is no PHP code running. PHP is not in the browser...PHP is on the server, waiting to process HTTP requests sent from the browser. – ADyson May 03 '23 at 08:54
  • This is a fundamental concept about how the web (and distributed, stateless client-server applications in general) operate which you need to get your head round in order to succeed in programming these kinds of applications. – ADyson May 03 '23 at 08:55
  • Of course, if all you want to do is turn it into a variable, you _could_ turn it into a _JS_ variable by using JSON.parse(), but it's not clear what you are actually planning to do with the data after you've decoded it, and/or whether that task would require PHP to be involved, or not. Some context might help us get you to the right solution. – ADyson May 03 '23 at 09:01
  • The data was generate on page1.php and stored in localStorage. I have to read the data from page2.php. The page itself should be generated by PHP and not JS (this is the requirement). My main problem is to get the data from localStorage into a PHP variable. In my case the string i receive return a object as an string. As soon as i try to output in json_encode($post) i return null. When i tried the same in JS, i will receive the JS object without any problems. –  May 03 '23 at 09:16
  • If the data is generated by PHP, and then must be re-used by PHP, then why did you put it into localStorage which requires JS to retrieve it? Why not put it into the PHP Session, or some other server-side storage (e.g. database or file) so that PHP has easy access to it? This has all the hallmarks of an [X-Y Problem](http://xyproblem.info/) now...e.g. you've made a design mistake, and instead of fixing the design mistake which is the root cause of the issue, you are looking for strange workarounds to it. – ADyson May 03 '23 at 09:22
  • `As soon as i try to output in json_encode($post) i return null`...yes, we've already explained why that is. $posts is a string of HTML and JS code, not a JSON object. if you `echo $posts` and then use the View Source feature in your browser (Ctrl+U in Chrome) you can see that it simply echoes the JS script into the page. It's the _execution of the JS script by the browser_ which causes the output you finally see in the browser window. But as we said above, that doesn't happen until after the PHP script has already finished, and has sent its own output to the browser. – ADyson May 03 '23 at 09:24
  • Thanks a lot! Yes, the solution works for me. I created an AJAX to store the data in PHP. I redirect to the new page and i have access to the session. Thanks a lot! –  May 03 '23 at 10:45

0 Answers0