简体   繁体   English

如何在 PHP 中获取 JavaScript 变量值

[英]How to get JavaScript variable value in PHP

I want the value of JavaScript variable which i could access using PHP.我想要我可以使用 PHP 访问的 JavaScript 变量的值。 I am using the code below but it doesn't return value of that variable in PHP.我正在使用下面的代码,但它不会在 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

Another php code i used which give empty value我使用的另一个 php 代码给出了空值

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

When I echo it shows nothing.当我回声时,它什么也没显示。

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 将 URL 发送回,其中包含一个变量,例如: http : //www.site.com/index.php?uid=1

by using something like this in JS:通过在 JS 中使用这样的东西:

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

Then in the PHP code use $_GET:然后在 PHP 代码中使用 $_GET:

$somevar = $_GET["uid"]; //puts the uid varialbe into $somevar

Add a cookie with the javascript variable you want to access.使用您要访问的 javascript 变量添加一个 cookie。

document.cookie="profile_viewer_uid=1";

Then acces it in php via然后通过 php 访问它

$profile_viewer_uid = $_COOKIE['profile_viewer_uid'];

Here is the Working example: Get javascript variable value on the same page.这是工作示例:在同一页面上获取 javascript 变量值。

<script>
var p1 = "success";
</script>

<?php
echo "<script>document.writeln(p1);</script>";
?>

You might want to start by learning what Javascript and php are.您可能想从学习什么是 Javascript 和 php 开始。 Javascript is a client side script language running in the browser of the machine of the client connected to the webserver on which php runs. Javascript 是一种客户端脚本语言,它运行在与运行 php 的网络服务器相连的客户端机器的浏览器中。 These languages can not communicate directly .这些语言不能直接交流。

Depending on your goal you'll need to issue an AJAX get or post request to the server and return a json/xml/html/whatever response you need and inject the result back in the DOM structure of the site.根据您的目标,您需要向服务器发出AJAX get 或 post 请求并返回 json/xml/html/whatever 您需要的响应并将结果注入回站点的 DOM 结构中。 I suggest Jquery, BackboneJS or any other JS framework for this.为此,我建议使用 Jquery、BackboneJS 或任何其他 JS 框架。 See the Jquery documentation for examples.有关示例,请参阅 Jquery 文档。

If you have to pass php data to JS on the same site you can echo the data as JS and turn your php data using json_encode() into JS.如果您必须在同一站点上将 php 数据传递给 JS,您可以将数据作为 JS 回显,并使用json_encode()将您的 php 数据转换为 JS。

<script type="text/javascript>
    var foo = <?php echo json_encode($somePhpVar); ?>
</script>

If you want to use a js variable in a php script you MUST pass it within a HTTP request.如果您想在 php 脚本中使用 js 变量,您必须在 HTTP 请求中传递它。

There are basically two ways:基本上有两种方式:

  • Submitting or reloading the page (as per Chris answer ).提交或重新加载页面(根据Chris 的回答)。
  • Using AJAX , which is made exactly for communicating between a web page (js) and the server(php) without reloading/changing the page.使用AJAX ,它专门用于在网页 (js) 和服务器 (php) 之间进行通信,而无需重新加载/更改页面。

A basic example can be:一个基本的例子可以是:

var profile_viewer_uid = 1;
$.ajax({
  url: "serverScript.php",
  method: "POST",
  data: { "profile_viewer_uid": profile_viewer_uid }
})

And in the serverScript.php file, you can do:serverScript.php文件中,您可以执行以下操作:

 $profile_viewer_uid = $_POST['profile_viewer_uid'];
 echo($profile_viewer_uid);
 // prints 1

Note: in this example I used jQuery AJAX , which is quicker to implement.注意:在这个例子中我使用了jQuery AJAX ,它实现起来更快。 You can do it in pure js as well.你也可以用纯js来做。

PHP runs on the server. PHP 在服务器上运行。 It outputs some text.它输出一些文本。 Then it stops running.然后它停止运行。

The text is sent to the client (a browser).文本被发送到客户端(浏览器)。 The browser then interprets the text as HTML and JavaScript.然后浏览器将文本解释为 HTML 和 JavaScript。

If you want to get data from JavaScript to PHP then you need to make a new HTTP request and run a new (or the same) PHP script.如果您想从 JavaScript 获取数据到 PHP,那么您需要发出一个新的 HTTP 请求并运行一个新的(或相同的)PHP 脚本。

You can make an HTTP request from JavaScript by using a form or Ajax.您可以使用表单或 Ajax 从 JavaScript 发出 HTTP 请求。

These are two different languages, that run at different time - you cannot interact with them like that.这是两种不同的语言,运行在不同的时间——你不能像那样与它们交互。

PHP is executed on the server while the page loads. PHP 在页面加载时在服务器上执行。 Once loaded, the JavaScript will execute on the clients machine in the browser.加载后,JavaScript 将在浏览器中的客户端计算机上执行。

In your html form make a hidden field在您的 html 表单中创建一个隐藏字段

<input type="hidden" id="scanCode" name="SCANCODE"></input>

Then in your javascript update the field value by adding;然后在您的 javascript 中通过添加更新字段值;

document.getElementById("scanCode").setAttribute('value', scanCode);

This could be a little tricky thing but the secure way is to set a javascript cookie, then picking it up by php cookie variable.Then Assign this php variable to an php session that will hold the data more securely than cookie.Then delete the cookie using javascript and redirect the page to itself.这可能有点棘手,但安全的方法是设置一个 javascript cookie,然后通过 php cookie 变量获取它。然后将此 php 变量分配给一个 php 会话,该会话将比 cookie 更安全地保存数据。然后删除 cookie使用 javascript 并将页面重定向到自身。 Given that you have added an php command to catch the variable, you will get it.鉴于您添加了一个 php 命令来捕获变量,您将获得它。

You need to add this value to the form data that is submitted to the server.您需要将此值添加到提交给服务器的表单数据中。 You can use您可以使用

<input type="hidden" value="1" name="profile_viewer_uid" id="profile_viewer_uid">

inside your form tag.在您的表单标签内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM