Javascript 如何将javascript变量值分配给php变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10207926/
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
How to assign the javascript variable value to php variable
提问by Muhammad Usman
Possible Duplicate:
How to pass JavaScript variables to PHP?
I want to assign the javascript variable to php variable
我想将 javascript 变量分配给 php 变量
$msg = "<script>document.write(message)</script>";
$f = new FacebookPost;
$f->message = $msg;
But it is not working......
但它不起作用......
回答by Simon Forsberg
PHP is executed before javascript. You can't do that. PHP is server based, JavaScript is client based.
PHP 在 javascript 之前执行。你不能那样做。PHP 是基于服务器的,JavaScript 是基于客户端的。
When PHP is finished executing, it outputs HTML. In the HTML there can be JavaScript code also. So PHP can 'control' JavaScript by outputting JavaScript code to the page, but not the other way around. You'd have to use Ajax-call for that.
当 PHP 完成执行时,它会输出 HTML。在 HTML 中也可以有 JavaScript 代码。因此 PHP 可以通过将 JavaScript 代码输出到页面来“控制” JavaScript,但反过来不行。为此,您必须使用 Ajax 调用。
回答by Armatus
Javascript works on the user's computer, PHP works on your server. In order to send variables from JS to PHP and vice versa, you need communication such as a page load.
Javascript 在用户的计算机上运行,PHP 在您的服务器上运行。为了将变量从 JS 发送到 PHP,反之亦然,您需要进行诸如页面加载之类的通信。
Have a look here on what you can do: How to pass JavaScript variables to PHP?or more specifically the first answer (https://stackoverflow.com/a/1917626/1311593)
看看这里你可以做什么:如何将 JavaScript 变量传递给 PHP?或更具体地说,第一个答案(https://stackoverflow.com/a/1917626/1311593)
回答by Starx
Javascript, is a client side script, Where as PHP
is a server side script.
Javascript,是客户端脚本,而PHP
服务器端脚本也是如此。
The only way you can send data to PHP is through redirection
or ajax
.
向 PHP 发送数据的唯一方法是通过redirection
或ajax
。
回答by Iso
You cannot do that this way. You have to transmit values via$_GET
, $_POST
or $_COOKIE
, afterthe script has been executed by client.
你不能这样做。你要发送的值通过$_GET
,$_POST
或者$_COOKIE
,之后的剧本已经由客户端执行。