php 将javascript变量值发送到同一页面内的php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16205652/
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
sending javascript variable value to php within the same page
提问by jonas
I just want to know if sending value of a javascript to php is possible. here is the simple code of what i am trying to say.
我只想知道是否可以将 javascript 的值发送到 php。这是我想说的简单代码。
<script language="javascript" >
var id = "data"
</script>
<?php
$getthevalueofid = var id;
?>
thanks!
谢谢!
回答by Justin Helgerson
Not the way your code sample is structured, no. PHP is a server-side language and Javascript is a client-side language. PHP is out of scope by the time any client-side script executes.
不是您的代码示例的结构方式,不是。PHP 是一种服务器端语言,而 Javascript 是一种客户端语言。当任何客户端脚本执行时,PHP 已超出范围。
If you need to pass an object from the browser to your server you can use XMLHttpRequest (commonly referred to as AJAX).
如果您需要将对象从浏览器传递到您的服务器,您可以使用 XMLHttpRequest(通常称为 AJAX)。
回答by SydneyAustraliaDev
To send a Javascript variable back to PHP, you need to do an AJAX request:
要将 Javascript 变量发送回 PHP,您需要执行 AJAX 请求:
<script language="javascript" >
xmlhttp.open("GET","getvalue.php?id="+id,true);
xmlhttp.send();
</script>
And in getvalue.php you'd have:
在 getvalue.php 中,您将拥有:
<?php
$getthevalueofid = $_GET['id'];
?>
回答by Tamil Selvan C
No way to do this. Use Ajax to pass javascript variable to php
没有办法做到这一点。使用 Ajax 将 javascript 变量传递给 php