如何将 JavaScript 变量输出存储到 PHP 变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14662927/
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 can I store JavaScript variable output into a PHP variable?
提问by logan
I have the following code in JavaScript:
我在 JavaScript 中有以下代码:
<script>
var a="Hello";
</script>
PHP Code:-
PHP代码:-
<?php
$variable = // I want the above JavaScript variable 'a' value to be stored here
?>
Note : I don't want it in a form submission. I have some logic in JavaScript and I want to use it in the same PHP page... Please let me know how I can do this.
注意:我不希望在表单提交中使用它。我在 JavaScript 中有一些逻辑,我想在同一个 PHP 页面中使用它...请让我知道如何做到这一点。
回答by JAAulde
You have to remember that if JS and PHP live in the same document, the PHP will be executed first (at the server) and the JS will be executed second (at the browser)--and the two will NEVER interact (excepting where you output JS with PHP, which is not really an interaction between the two engines).
你必须记住,如果 JS 和 PHP 存在于同一个文档中,PHP 将首先执行(在服务器上),而 JS 将第二次执行(在浏览器上)——并且两者永远不会交互(除了你在哪里)用 PHP 输出 JS,这并不是真正的两个引擎之间的交互)。
With that in mind, the closest you could come is to use a PHP variable in your JS:
考虑到这一点,您最接近的是在您的 JS 中使用 PHP 变量:
<?php
$a = 'foo'; // $a now holds PHP string foo
?>
<script>
var a = '<?php echo $a; ?>'; //outputting string foo in context of JS
//must wrap in quotes so that it is still string foo when JS does execute
//when this DOES execute in the browser, PHP will have already completed all processing and exited
</script>
<?php
//do something else with $a
//JS still hasn't executed at this point
?>
As I stated, in this scenario the PHP (ALL of it) executes FIRST at the server, causing:
正如我所说,在这种情况下,PHP(全部)首先在服务器上执行,导致:
- a PHP variable
$ato be created as string 'foo' - the value of
$ato be outputted in context of some JavaScript (which is not currently executing) - more done with PHP's
$a - all output, including the JS with the var assignment, is sent to the browser.
$a要创建为字符串 'foo'的 PHP 变量$a要在某些 JavaScript 的上下文中输出的值(当前未执行)- 使用 PHP 完成更多工作
$a - 所有输出,包括带有 var 赋值的 JS,都被发送到浏览器。
As written, this results in the following being sent to the browser for execution (I removed the JS comments for clarity):
正如所写,这会导致以下内容被发送到浏览器以执行(为了清楚起见,我删除了 JS 注释):
<script>
var a = 'foo';
</script>
Then, and only then, will the JS start executing with its own variable aset to "foo" (at which point PHP is out of the picture).
然后,并且只有在那时,JS 才会开始执行,并将其自己的变量a设置为“foo”(此时 PHP 不在画面中)。
In other words, if the two live in the same document and no extra interaction with the server is performed, JS can NOT cause any effect in PHP. Furthermore, PHP is limited in its effect on JS to the simple ability to output some JS or something in context of JS.
换句话说,如果两者生活在同一个文档中并且没有与服务器进行额外的交互,那么 JS 就不会对 PHP 产生任何影响。此外,PHP 对 JS 的影响仅限于输出一些 JS 或 JS 上下文中的东西的简单能力。
回答by Chetan Ghadiya
<html>
<head>
<script>
var a="Hello";
</script>
</head>
<body>
<?php
echo $variable = "<script>document.write(a)</script>"; //I want above javascript variable 'a' value to be store here
?>
</body>
回答by Lix
The ideal method would be to pass it with an AJAX call, but for a quick and dirty method, all you'd have to do is reload the page with this variable in a $_GETparameter -
理想的方法是通过 AJAX 调用传递它,但对于快速而肮脏的方法,您所要做的就是在$_GET参数中使用此变量重新加载页面-
<script>
var a="Hello";
window.location.href = window.location.href+'?a='+a;
</script>
Your page will reload and now in your PHP, you'll have access to the $_GET['a']variable.
您的页面将重新加载,现在在您的 PHP 中,您将可以访问该$_GET['a']变量。
<?php
$variable = $_GET['a'];
?>
回答by Manjula
If this is related to a form submission, use a hidden inputinside the form and change the hidden input value to this variable value. Then you can get that hidden input value in the php page and assign it to your php variable after form submission.
如果这与表单提交有关,请在表单内使用隐藏输入并将隐藏输入值更改为该变量值。然后,您可以在 php 页面中获取隐藏的输入值,并在提交表单后将其分配给您的 php 变量。
Update:
更新:
According to your edit, it seems you don't understand how javascript and php works. Javascript is a client side language, and php is a serverside language. Therefore you cannot execute javascript logic and use that variable value to a php variable when you execute relevant page in the server. You can run the relevant javascript logic after client browser process the web page returned from the web server (which has already executed the php code for the relevant page). After the execution of the javascript code and after assigning the relevant value to the relevant javascript variable, you can use form submission or ajax to send that javascript variable value to use by another php page (or a request to process and get the same php page).
根据您的编辑,您似乎不了解 javascript 和 php 的工作原理。Javascript 是一种客户端语言,而 php 是一种服务器端语言。因此,当您在服务器中执行相关页面时,您无法执行 javascript 逻辑并将该变量值用于 php 变量。您可以在客户端浏览器处理从 Web 服务器返回的网页后运行相关的 javascript 逻辑(已经执行了相关页面的 php 代码)。执行完javascript代码并将相关值分配给相关javascript变量后,您可以使用表单提交或ajax发送该javascript变量值以供另一个php页面使用(或处理并获取相同php页面的请求) )。
回答by Iron shield
JavaScript variable = PHP variable try follow:-
JavaScript 变量 = PHP 变量尝试遵循:-
<script>
var a="Hello";
<?php
$variable='a';
?>
</script>
Note:-It run only when you do php code under script tag.I have a successfully initialise php variable.
注意:-它仅在您在脚本标记下执行 php 代码时运行。我成功初始化了 php 变量。
回答by Da Flo
You can solve this problem by using AJAX. You don't need to load JQuery for AJAX but it has a better error and success handling than native JS.
您可以使用 AJAX 解决此问题。您不需要为 AJAX 加载 JQuery,但它比原生 JS 具有更好的错误和成功处理能力。
I would do it like so:
我会这样做:
1) add an click eventlistener to all my anchors on the page. 2) on click, you can setup an ajax-request to your php, in the POST-DATA you set the anchor id or the text-value 3) the php gets the value and you can setup a request to your database. Then you return the value which you need and echo it to the ajax-request. 4) your success function of the ajax-request is doing some stuff
1)为页面上的所有锚点添加一个点击事件侦听器。2)点击后,您可以设置对 php 的 ajax 请求,在 POST-DATA 中设置锚 ID 或文本值 3)php 获取该值,您可以设置对数据库的请求。然后返回所需的值并将其回显到 ajax 请求。4)你的ajax请求的成功功能正在做一些事情
For more information about ajax-requests look back here:
有关 ajax 请求的更多信息,请查看此处:
-> Ajax-Request NATIVE https://blog.garstasio.com/you-dont-need-jquery/ajax/
-> Ajax-Request NATIVE https://blog.garstasio.com/you-dont-need-jquery/ajax/
A simple JQuery examle:
一个简单的 JQuery 示例:
$("button").click(function(){
$.ajax({url: "demo_test.txt", success: function(result){
$("#div1").html(result);
}});
});
回答by dmayo
You really can't. PHP is generated at the server then sent to the browser, where JS starts to do it's stuff. So, whatever happens in JS on a page, PHP doesn't know because it's already done it's stuff. @manjula is correct, that if you want that to happen, you'd have to use a POST, or an ajax.
你真的不能。PHP 在服务器上生成然后发送到浏览器,在那里 JS 开始做它的东西。所以,无论页面上的 JS 发生了什么,PHP 都不知道,因为它已经完成了。@manjula 是正确的,如果您希望发生这种情况,则必须使用 POST 或 ajax。

