将HTML5本地存储值传递给PHP错误

Sat*_*000 4 javascript php local-storage

我正在使用html5本地存储,我正在尝试读取它并将其传递给php变量:

这是代码:

$myphpvar = "<script>document.write(localStorage.getItem('myjsvar'));</script>"; 
Run Code Online (Sandbox Code Playgroud)

当我这样做:

echo $myphpvar;
Run Code Online (Sandbox Code Playgroud)

该值看起来正确(在视觉上离开)

当我添加此代码时,所有看起来都很好但是

$sql="INSERT INTO `pending` (`id`, `myfield`) VALUES ('', '$myphpvar')";
Run Code Online (Sandbox Code Playgroud)

然后我得到这个错误:

错误:SQL语法中有错误; 检查与您的MySQL服务器版本对应的手册,以便在附近使用正确的语法.

错误点在这里:

$myphpvar = "<script>document.write(localStorage.getItem('myjsvar'));</script>";
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

Ash*_*hra 9

更新 :

这不起作用,因为:

$myphpvar = "<script>document.write(localStorage.getItem('myjsvar'));</script>"; 
Run Code Online (Sandbox Code Playgroud)

现在您的PHP $myphpvar变量包含:

  <script>document.write(localStorage.getItem('myjsvar'));</script>
Run Code Online (Sandbox Code Playgroud)

当你echo这时,这就像:

echo "<script>document.write(localStorage.getItem('myjsvar'));</script>"
Run Code Online (Sandbox Code Playgroud)

所以这将显示你的Js变量,因为它在你的浏览器上运行.

但是当你在SQL中执行此操作时:它看起来如下所示:

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

为实现这一目标,您必须将您的localStorage价值传递给URL它,然后将其用于发布PHP或用于AJAX发布!

window.location.href = window.location.href+"?local="+localStorage.getItem('myjsvar'));
Run Code Online (Sandbox Code Playgroud)