4

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?

Satch3000
  • 47,356
  • 86
  • 216
  • 346
  • 1
    There is a difference ... When I echo the variable I can see it's there in PHP, so it's been passed. – Satch3000 Mar 24 '14 at 12:57
  • No, when you echo the variable you are echoing the JS. **When it gets to the browser**, the JS runs and changes the document so you can see the contents of the variable. – Quentin Mar 24 '14 at 12:57
  • This question appears to be off-topic because it is reflects such a profound misunderstanding of the relationship between PHP and JavaScript. –  Mar 24 '14 at 16:59

4 Answers4

11

Updated :

This doesn't Work because :

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

Now your PHP $myphpvar variable contains :

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

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'));
ashbuilds
  • 1,401
  • 16
  • 33
0

You shouldn't do that because Php work on server side wherease the html5 works on client side. You certainly should certainly use js with ajax.

jadok
  • 113
  • 11
0

php is a back-end programming language. it communicates with your server and compiles code. localstorage is a front end object which is working on the users browser. You can not get that value as a php variable.

but a clue, you can put it on a form as a hidden input, and simulate form post. on submit of the post, you can send that value as form data, or a query string. so you can get it from there.

for your way, it is impossible.

cenk ebret
  • 687
  • 4
  • 15
-1

I think something might be wrong in your SQL query.

Try changing it to:

$sql="INSERT INTO `pending` (`id`, `myfield`) VALUES ('', '{$myphpvar}')";
Parrotmaster
  • 647
  • 1
  • 7
  • 27
  • @Satch3000 I think the problem might be the syntax then. You are trying to put a HTML5 localtorage string into an SQL query. There might be some characters that break the SQL query. Your best bet would be to show the localstorage result on the screen and then manually enter the SQL query with the localstorage string into the SQL line for your database. This way you can see if the SQL is not being destroyed by the implemented string. – Parrotmaster Mar 24 '14 at 13:23