Passing HTML5 local storage value in PHP

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

This is the code:

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

When I do this:

echo $myphpvar;

The value looks correct (with visual holiday)

Upto there everything looks good, but when I add this code:

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

Then I get this error:

Error: you have an error in the SQL syntax; check the manual that matches the version of your MySQL server for the correct syntax to be used nearby.

The error is here:

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

Any ideas why?

+4
source share
4 answers

Updated:

This does not work because:

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

Your PHP variable now $myphpvarcontains:

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

when you are echo, it looks like:

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

Js, .

SQL: :

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

localStorage URL PHP AJAX !

window.location.href = window.location.href+"?local="+localStorage.getItem('myjsvar'));
+9

, Php , html5 . , , js ajax.

0

php - . . localstorage - , . php.

, . . .

, .

0
source

I think something might be wrong in your SQL query.

Try changing it to:

$sql="INSERT INTO `pending` (`id`, `myfield`) VALUES ('', '{$myphpvar}')";
-1
source

Source: https://habr.com/ru/post/1533163/


All Articles