Asked  7 Months ago    Answers:  5   Viewed   325 times

I am using html5 local storage and I am trying to read it and pass it to a php variable:

This is the code:

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

When I do this:

echo $myphpvar;

The value looks right (at leave visually)

Upto there all looks good BUT when I add this code:

$sql="INSERT INTO `pending` (`id`, `myfield`) VALUES ('', '$myphpvar')";

I then get this error:

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ..

The error points here:

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

Any ideas why?



Updated :

This doesn't Work because :

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

Now your PHP $myphpvar variable contains :


when you echo then this is like :

echo "<script>document.write(localStorage.getItem('myjsvar'));</script>"

so this will show your Js variable,because it runs on your browser.

but when you do this in SQL : it look something like below :

$sql="INSERT INTO `pending` (`id`, `myfield`) VALUES ('', '<script>document.write(localStorage.getItem('myjsvar'));</script>')";

For Achieving this, you have to pass your localStorage value to URL,and get it on PHP or use AJAX to post!

window.location.href = window.location.href+"?local="+localStorage.getItem('myjsvar'));
Wednesday, March 31, 2021
answered 7 Months ago

localStorage and sessionStorage both extend Storage. There is no difference between them except for the intended "non-persistence" of sessionStorage.

That is, the data stored in localStorage persists until explicitly deleted. Changes made are saved and available for all current and future visits to the site.

For sessionStorage, changes are only available per tab. Changes made are saved and available for the current page in that tab until it is closed. Once it is closed, the stored data is deleted.

Tuesday, June 1, 2021
answered 5 Months ago

I use PersistJS (github repository), which handles client-side storage seamlessly and transparently to your code. You use a single API and get support for the following backends:

  • flash: Flash 8 persistent storage.
  • gears: Google Gears-based persistent storage.
  • localstorage: HTML5 draft storage.
  • whatwg_db: HTML5 draft database storage.
  • globalstorage: HTML5 draft storage (old spec).
  • ie: Internet Explorer userdata behaviors.
  • cookie: Cookie-based persistent storage.

Any of those can be disabled—if, for example, you don't want to use cookies. With this library, you'll get native client-side storage support in IE 5.5+, Firefox 2.0+, Safari 3.1+, and Chrome; and plugin-assisted support if the browser has Flash or Gears. If you enable cookies, it will work in everything (but will be limited to 4 kB).

Wednesday, June 2, 2021
answered 5 Months ago

When you are opening the files locally, i.e. using the file:// protocol, as of now the browsers can not determine what is "same domain" so every file is considered a separate domain. Thus you can not use localStorage when you're opening the files.

Here is some more information on the problem in FireFox: . Personally I couldn't find much about Safari on this topic.

You can also look over this: Javascript/HTML Storage Options Under File Protocol (file://) . It might be helpful in your situation.

Tuesday, June 15, 2021
answered 4 Months ago

I don't know why you'd want that, I would not recommend it, but you can do it using toString.

Store it:

var myFunc = function (){
  alert('Hello world!');

// Store it as a String
localStorage.setItem('compressedFunc', myFunc.toString());

Later, retrieve it:

var compressedFunc = localStorage.getItem('compressedFunc');

// Convert the String back to a function
var myFunc = eval('(' + compressedFunc + ')');

// Use it
Tuesday, August 10, 2021
Alan Clark
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :