如何在PHP中获取JavaScript变量值 [英] How to get JavaScript variable value in PHP

查看:134
本文介绍了如何在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

这篇关于如何在PHP中获取JavaScript变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆