Tony D
Tony D

Reputation: 25

Correct way to initialize localstorage?

I am creating an app for google chrome that makes heavy use of local storage and so far worked wonderfully, that is until I cleaned chrome's cache and now no matter what, is not setting any local storage variables where before the cache cleaning it worked beautifully.

I have a file called valtransfer.php that receives php values via something like this:

$_SESSION['id_area']=$_REQUEST['id_area'];

and then later down the file after the mandatory $(document.ready(function ()) bla bla I have a line like this to pass the values from the php session variable to a local storage:

localStorage.setItem("id_area",'<?php echo $_SESSION['id_area']?>');

As previously said, it worked beautifully... until I cleared cache to test the application, is there something I am missing in order to prepare the browser to work with localstorage like initializing or something?

Upvotes: 0

Views: 4446

Answers (2)

Sushant
Sushant

Reputation: 125

Try like this

localStorage.setItem("id_area","<?php echo $_SESSION['id_area']?>");

Upvotes: 1

Sudharsan S
Sudharsan S

Reputation: 15393

remove single quote and try like this. because php echo treat as a string

var sessionId =  <?php echo $_SESSION['id_area']?>;

localStorage.setItem("id_area", sessionId);

Upvotes: 1

Related Questions