小编典典

如何在PHP中获取JavaScript变量值

javascript

我想要可以使用PHP访问的JavaScript变量的值。我正在使用下面的代码,但它没有在PHP中返回该变量的值。

// set global variable in javascript
    profile_viewer_uid = 1;

// php code

$profile_viewer_uid=$_POST['profile_viewer_uid'];

这给了我以下错误:-

A PHP Error was encountered
Severity: Notice
Message: Undefined index: profile_viewer_uid

我使用的另一个PHP代码给出了空值

$profile_viewer_uid = "<script language=javascript>document.write(profile_viewer_uid);</script>

当我回声时,它什么也没显示。


阅读 316

收藏
2020-05-01

共1个答案

小编典典

您将需要使用JS将其中包含变量的URL发送回去,例如:http :
//www.site.com/index.php?uid=1

通过在JS中使用如下代码:

window.location.href=”index.php?uid=1";

然后在PHP代码中使用$ _GET:

$somevar = $_GET["uid"]; //puts the uid varialbe into $somevar
2020-05-01