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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22609986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:27:46  来源:igfitidea点击:

Passing HTML5 Local Storage Value to PHP error

javascriptphplocal-storage

提问by Satch3000

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

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

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

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

The error points here:

错误点在这里:

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

Any ideas why?

任何想法为什么?

回答by ashbuilds

Updated :

更新 :

This doesn't Work because :

这不起作用,因为:

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

Now your PHP $myphpvarvariable contains :

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

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

when you echothen this is like :

当你echo然后这就像:

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

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

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

but when you do this in SQL : it look something like below :

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

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

For Achieving this, you have to pass your localStoragevalue to URL,and get it on PHPor use AJAXto post!

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

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

回答by jadok

You shouldn't do that because Php work on server side wherease the html5 works on client side. You certainly should certainly use js with ajax.

你不应该这样做,因为 PHP 在服务器端工作,而 html5 在客户端工作。您当然应该将 js 与 ajax 一起使用。

回答by cenk ebret

php is a back-end programming language. it communicates with your server and compiles code. localstorage is a front end object which is working on the users browser. You can not get that value as a php variable.

php 是一种后端编程语言。它与您的服务器通信并编译代码。localstorage 是在用户浏览器上工作的前端对象。您无法将该值作为 php 变量获取。

but a clue, you can put it on a form as a hidden input, and simulate form post. on submit of the post, you can send that value as form data, or a query string. so you can get it from there.

但是一个线索,您可以将其作为隐藏输入放在表单上,​​并模拟表单发布。在提交帖子时,您可以将该值作为表单数据或查询字符串发送。所以你可以从那里得到它。

for your way, it is impossible.

为了你的方式,这是不可能的。

回答by Parrotmaster

I think something might be wrong in your SQL query.

我认为您的 SQL 查询可能有问题。

Try changing it to:

尝试将其更改为:

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