将 PHP 变量转换为 JavaScript 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19497583/
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
Converting a PHP variable to a JavaScript variable
提问by user2872498
I am making a pop up window with JavaScript using window.open()
. The page to be opened should be stored in a variable.
我正在使用 JavaScript 制作一个弹出窗口window.open()
。要打开的页面应该存储在一个变量中。
That variable's name is determined using a PHP <form>
. When the user clicks "confirm", it brings them to play.php?name=userinput
, and when the window.open
function is executed, it currently opens txtone.php
. But I want it to open txtone.php?name=userinput
, including that argument.
该变量的名称是使用 PHP 确定的<form>
。当用户点击“确认”时,它会将他们带到play.php?name=userinput
,并且当该window.open
功能被执行时,它当前打开txtone.php
。但我希望它 open txtone.php?name=userinput
,包括那个论点。
I can retrieve the value from the parent window (name=userinput
), using a PHP variable, but I need to transform it into a JavaScript variable, and then use window.open(name);
(The variable's name is name
).
我可以name=userinput
使用 PHP 变量从父窗口 ( )检索值,但我需要将其转换为 JavaScript 变量,然后使用window.open(name);
(变量的名称是name
)。
So I am basically asking how do I convert a PHP variable into a JavaScript variable.
所以我基本上是在问如何将 PHP 变量转换为 JavaScript 变量。
回答by kainaw
Making a PHP variable available in JavaScript happens when the page is generated. For example, assume you have a php variable called $name and you want a JavaScript variable called name. You normally define your JavaScript variable as:
在生成页面时,使 JavaScript 中的 PHP 变量可用。例如,假设您有一个名为 $name 的 php 变量,并且您想要一个名为 name 的 JavaScript 变量。您通常将 JavaScript 变量定义为:
var name='some value';
Instead, you will insert PHP:
相反,您将插入 PHP:
var name='<?php echo $name; ?>';
Now, the value of $name in your PHP is also the value of name in your JavaScript at the moment that the page is dynamically created.
现在,PHP 中 $name 的值也是动态创建页面时 JavaScript 中 name 的值。
回答by Shashank Saxena
Try this :
试试这个 :
where spge
is variable in js
其中spge
变量在js
var spge = '<?php echo $status ;?>';
回答by Gualter Costa
//suppose
//认为
<?php $tochange="from php to js" ?>
some where
有些地方
//Head
<script>
function change(){
var temp = document.getElementById("change").innerHTML;
alert ("temp");
}
</script>
//body
<body onload="change()" >
//create a invisible tag in body php document
<span id="change"><?php echo $tochange ?></span>
.....