Thursday, December 27, 2018

javascript - Passing HTML5 Local Storage Value to PHP error




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 = ""; 


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 = "";


Any ideas why?


Answer



Updated :




This doesn't Work because :



$myphpvar = ""; 


Now your PHP $myphpvar variable contains :



  



when you echo then this is like :



echo ""


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 ('', '')";



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

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...