javascript 将javascript变量传递给php?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5546675/
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
Passing javascript variables to php?
提问by Trevor Rudolph
ok so im trying to get a javascript variable into a php script, this would be an example,
好的,所以我试图将一个 javascript 变量放入一个 php 脚本中,这将是一个例子,
<script type="text/javascript">
x = new Date()
</script>
<?php
$x = $_GET["x"];
?>
<p><?php echo $x ?></p>
i just cant get it to work! I need help please?
我只是无法让它工作!我需要帮助吗?
EDIT: well im just trying to get the hash from a url via javascript then put that in a php script.
编辑:好吧,我只是想通过 javascript 从 url 获取哈希,然后将其放入 php 脚本中。
回答by Khez
PHP and javascript don't work like that.
PHP 和 javascript 不能那样工作。
PHP is a server-side language. While javascript is a clientside language.
PHP 是一种服务器端语言。虽然 javascript 是一种客户端语言。
There are still ways of passing data from the client-window to your server, via ajax, query parameters, cookies... But none will work in the same page.
仍然有一些方法可以将数据从客户端窗口传递到您的服务器,通过 ajax、查询参数、cookies……但没有一种方法可以在同一页面中工作。
Give us a clearer image on what you are trying to achieve and we will gladly help.
让我们更清楚地了解您要实现的目标,我们将很乐意提供帮助。
UPDATE
更新
JS
JS
<script type="text/javascript">
document.cookie = 'name=Khez' ;
</script>
PHP
PHP
<?php
var_dump($_COOKIE['name']);
?>
回答by Raja Krishnan
So there are 2 pages page1.php
and page2.php
所以有 2 页page1.php
和page2.php
page2.php
needs to pass JS variables to page1.php
page2.php
需要将 JS 变量传递给 page1.php
We can do this by passing it as url variables from page2.php
and get it in page1.php
using $_GET[]
.
我们可以通过将它作为 url 变量传递来实现这一点,page2.php
并在page1.php
using 中获取它$_GET[]
。
page2.php
(send JS variable)
page2.php
(发送JS变量)
<script type=text/javascript>
var lati = location.lat();
var longi = location.lng();
document.location = 'http://www.rajak.me/index.php?addlat='+lati+'&addlong='+longi;
});
</script>
page1.php
(receive JS variable)
page1.php
(接收JS变量)
<?php
$addlat = $_GET['addlat'];
$addlong = $_GET['addlong'];
?>
回答by Dcdanny
You could use the following Javascript which will link to somepage.phpwith the variable in the Url
您可以使用以下 Javascript 将链接到somepage.php与 Url 中的变量
<script type="text/javascript">
x = new Date()
window.location.href = "somepage.php?w1=" + x;
</script>
This is the contents of somepage.phpwhich recieves the variable and echoes it
这是somepage.php的内容,它接收变量并回显它
<?php
if (isset($_GET["w1"])) {
$x = $_GET["w1"];
echo $x;
}else{
echo 'no variable received';
}
?>
回答by John Sobolewski
PHP is "Server Side" code and javascript is client side code. They don't interact...
PHP 是“服务器端”代码,javascript 是客户端代码。他们不互动...
回答by Shad
PHP is server-side~ all parsing is done on the server. JavaScript is client-side~ everything happens AFTER it get's to the client. If you need date in PHP, I recommend time()
and or date()
PHP是服务器端的~所有的解析都是在服务器端完成的。JavaScript 是客户端的~一切都在它到达客户端之后发生。如果您需要 PHP 中的日期,我推荐time()
和或date()