[英]PHP Get Value from JavaScript Variable
I have an Index.php file, which contain a simple js,php language for getting a value from javascript, but I can't do that. 我有一个Index.php文件,其中包含用于从javascript获取值的简单js,php语言,但我无法做到这一点。 can you tell me about how to do it?
你能告诉我怎么做吗?
Here is My index.php Wrong Syntax File: 这是我的index.php错误的语法文件:
<html>
<body>
<script>
var foo = "bar";
</script>
<?php
$fromJS = "<script>var jsfoo = foo;</script>";
echo $fromJS;
?>
</body>
</html>
If I'm right you want to set a variable in JavaScript and read its value in PHP. 如果我是对的,您想在JavaScript中设置一个变量并在PHP中读取其值。 If that's right, it is not possible that way.
如果是的话,那是不可能的。 The reason is really simple: PHP will be executed inside the HTTP server on the server side, while JavaScript is executed in the browser on the client side.
原因很简单:PHP将在服务器端的HTTP服务器内部执行,而JavaScript在客户端的浏览器中执行。
To pass the value of a JavaScript variable to PHP you have to initiate a request to your PHP script and add the value to the PSOT or GET request. 要将JavaScript变量的值传递给PHP,您必须向您的PHP脚本发起一个请求,并将该值添加到PSOT或GET请求中。
A very simple example: 一个非常简单的例子:
The JavaScript part JavaScript部分
var foo = 'bar';
window.location.href = '/path/to/script.php?foo=' + foo;
And the PHP part 和PHP部分
<?php
echo htmlentities($_REQUEST['foo']);
I hope that helps. 希望对您有所帮助。
You can't do this, at least not the way you have here . 您无法做到这一点, 至少不是这里的方式 。
PHP is a server-side language, and thus is executed as the page is rendered to the response, which is then passed back to the client (the browser). PHP是一种服务器端语言,因此在将页面呈现给响应时执行,然后将其传递回客户端(浏览器)。
JavaScript is a client-side language, and isn't executed until it is read by the browser. JavaScript是一种客户端语言,在浏览器读取之前不会执行。 Therefore, it is not possible to pass the client-side data (the JavaScript) to the server (PHP) as you have it here .
因此,无法像在此处将客户端数据(JavaScript)传递到服务器(PHP) 一样 。
The only way you can achieve this is to have another page which POSTs or GETs data to a PHP via JavaScript (using a form submit or an XMLHTTPRequest, etc), and then process the posted data from within PHP that way. 实现此目的的唯一方法是拥有另一个页面,该页面通过JavaScript(使用表单提交或XMLHTTPRequest等)将数据发布或获取到PHP,然后以这种方式处理PHP中发布的数据。
Here's a really simple example which performs a GET against the PHP page: 这是一个非常简单的示例,对PHP页面执行GET:
On the client side (JavaScript): 在客户端(JavaScript):
window.location.href = "http://yoursite.com/page.php?foo=bar";
On the server side (PHP): 在服务器端(PHP):
<?php
echo htmlspecialchars($_REQUEST['foo']);
?>
You can create a hidden iframe and a submit a form within the iframe with the variable you want to send to your script. 您可以创建一个隐藏的iframe,并在iframe中使用要发送到脚本的变量提交表单。 Also all DOM manipulations can be done from the iframe.
同样,所有DOM操作都可以通过iframe完成。
<html>
<body>
<iframe id="ifrm" src="iframe.php" width="1" height="1" style="display: none;"></iframe>
<script>
var foo = "bar";
// I assume you are using jQuery
jQuery('ifrm').contents().find('#inpt').val(foo);
jQuery('ifrm').contents().find('form').submit();
</script>
</body>
</html>
iframe.php iframe.php
<html>
<!-- include jQuery or whatever -->
<body>
<?php
if(isset($_POST['inpt'])) {
// Do your manipulations here
echo '<script type="text/javscript">';
// retrieve iframes parent (your webpage)
echo 'let parent = window.parent.document.body';
// do anything you want
echo 'parent.style.backgroundColor = \'#ff0000\'';
echo '</script>';
}
?>
<form action="#" method="post" enctype="multipart/form-data">
<input id="inpt" name="inpt" type="hidden" value="" />
</form>
</body>
</html>
Should work like this, but keep in mind - I haven't tested the above code. 应该像这样工作,但请记住-我尚未测试以上代码。 But you should get the idea.
但是你应该明白这个想法。
Have fun with it. 玩得开心。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.