将HTML5本地存储值传递给PHP错误 [英] Passing HTML5 Local Storage Value to PHP error

查看:68
本文介绍了将HTML5本地存储值传递给PHP错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是代码:

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

当我这样做时:

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

然后我收到此错误:

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

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 ..

错误点在这里:

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

任何想法为什么?

推荐答案

更新:

这不起作用,因为:

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

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

Now your PHP $myphpvar variable contains :

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

当你 echo 时,这就像:

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

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

so this will show your Js variable,because it runs on your browser.

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

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

要实现此目标 ,您必须将 localStorage 值传递给 URL ,并在 PHP 或使用 AJAX 发布!

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

这篇关于将HTML5本地存储值传递给PHP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆