1

I am trying to get sessionStorage data to a php string variable. Then from there I am trying to convert it to a php array of objects. My target is to save stored data in sessionStorage to a mysql database. Following is the process I am following.

$jsonString = "<script>document.write(sessionStorage['sca'])</script>";
$arr = json_decode($jsonString );
var_dump($arr );      
print_r($arr);

The Output I am getting is NULL

What I want is the following

Array ( [0] => stdClass 
        Object ( [name] => iphone [s] => black [price] => 50000 [id] => 2 [qty] => 3 ) 
        [1] => stdClass 
        Object ( [name] => iphone [s] => black [price] => 50000 [id] => 2 [qty] => 3 ) 
      )

Can't figure what I am doing wrong

Ben Hare
  • 4,365
  • 5
  • 27
  • 44
Abdullah
  • 11
  • 4
  • check it out: http://stackoverflow.com/questions/2338942/access-a-javascript-variable-from-php – Rodrigo Boratto Jan 05 '17 at 13:30
  • 1
    You are [decoding](http://php.net/manual/en/function.json-decode.php) a string, which, since it's not a json array, gives you null and probably also an exception. If you want to turn this string into a json element, try [encoding](http://php.net/manual/en/function.json-encode.php) it. – Peon Jan 05 '17 at 13:33
  • PHP pre-processes some code (php and html) and sends a page to the browser ... this is one way communication ... server -> client ... the two most common ways to send data from client to server is through a `
    ` submission or using AJAX
    – Jaromanda X Jan 05 '17 at 13:33

0 Answers0