Passing HTML5 Local Storage Value to PHP error [duplicate]

2024/2/27 7:22:03

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?

Answer

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'));
http://en.ppmy.cn/q/41504.html

Related Q&A

Calculate the mid point of latitude and longitude co-ordinates

Does anyone know the best way to go about getting the mid-point of a pair of latitude and longitude points?I mm using d3.js to draw points on a map and need to draw a curved line between two points, s…

Open a link from web app to new Safari window in iOS 8

UPDATE 07.14.15 Losing all hope... Im assuming my fix will be javascript related but I dont know it at all. Is my current code stopping my HTML target="Blanks" from working? Im making an HTM…

Async Generator: Yielding a rejected promise

Ive been playing around with async generators in an attempt to make a "promise ordering" generator which takes an array of promises and yields out promises one by one in the order they resolv…

JS get the clicked element with event.target

I am trying to use JavaScript to get a clicked elements information.Here is the jsFiddle. And below is my code.let div = document.querySelector(div)div.addEventListener(click, function(event) {// how t…

How do I programmatically download a file using the browsers native download widget in Angular?

So, when I request my webservice for getting download a zip file, it downloads the file content secretely and all of a sudden, the file appears in the download task bar but already downloaded full (100…

How to disable smooth scrolling in IE11

IEs smooth scrolling is causing my app behave strange (scroll events are fired with a small delay).Is there a way to completely disable smooth scrolling in IE11 using CSS or Javascript?

Removing rows when reading data D3

Say I have a sample file sample.csv:row,col,value 1,1,2 1,2,3 1,3,NAWhen reading data in d3 you do something like:d3.csv("sample.csv", function(data) {data.forEach(function(d) {d.value = +d.v…

Angularjs: ng-options: How to order options with group and non-group

I have my group by ng-options looks like this<select ng-model="selected" ng-options="d.title group by d.group for d in data"></select>Here is my data$scope.data = [{grou…

Making Live Clock javascript

does anyone know how to make live javascript time running.. i have this php code$expiredate = date(d m Y G:i:s, $rdate1);$f_ex_date = explode(" ", $expiredate);$f_ex_time = explode(":&qu…

Prevent zombie.js from loading only external resources

I am using zombie.js to load a page from a local express server during a test. Unfortunately, there is a script element which calls out to Google Analytics. I would like to block this external script (…