如何在PHP中获取JavaScript变量值
问题描述:
我想要使用PHP可以访问的JavaScript变量的值。
我使用下面的代码,但它不返回PHP中该变量的值。
I want the value of JavaScript variable which i could access using PHP. I am using the code below but it doesn't return value of that variable in PHP.
// set global variable in javascript
profile_viewer_uid = 1;
// php code
$profile_viewer_uid=$_POST['profile_viewer_uid'];
这给我以下错误: -
this gives me the following error :-
A PHP Error was encountered
Severity: Notice
Message: Undefined index: profile_viewer_uid
我使用的另一个PHP代码,它给出空值
Another php code i used which give empty value
$profile_viewer_uid = "<script language=javascript>document.write(profile_viewer_uid);</script>
当我回声它没有显示任何内容。
When I echo it shows nothing.
答
您需要使用JS发回带有变量的URL,例如:
http://www.site.com/index.php?uid=1
You will need to use JS to send the URL back with a variable in it such as: http://www.site.com/index.php?uid=1
在JS中使用类似的东西:
by using something like this in JS:
window.location.href="index.php?uid=1";
然后在PHP代码中使用$ _GET:
Then in the PHP code use $_GET:
$somevar = $_GET["uid"]; //puts the uid varialbe into $somevar