Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
6
rated 0 times [  13] [ 7]  / answers: 1 / hits: 18902  / 10 Years ago, mon, march 24, 2014, 12:00:00

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?


More From » php

 Answers
26

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'));

[#71811] Saturday, March 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brandensebastiand

Total Points: 323
Total Questions: 115
Total Answers: 106

Location: China
Member since Mon, Aug 22, 2022
2 Years ago
;