如何将JavaScript变量输出存储到PHP变量中? [英] How can I store JavaScript variable output into a PHP variable?

查看:233
本文介绍了如何将JavaScript变量输出存储到PHP变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中有以下代码:

 < script> 
var a =Hello;
< / script>

PHP代码: -

 <?php 
$ variable = //我想将上面的JavaScript变量'a'值存储在这里
?>

注意:我不希望它在表单提交中。我在JavaScript中有一些逻辑,我想在同一个PHP页面中使用它...请让我知道我该怎么做。

你必须记住,如果JS和PHP存在于同一个文档中,PHP将首先执行(在服务器上),JS将在第二个执行(在浏览器中) - 而这两者将不会(除了用PHP输出JS,这不是两个引擎之间的交互)。



考虑到这一点,最接近的就是使用在你的JS中有一个PHP变量:

 <?php 
$ a ='foo'; // $ a现在拥有PHP字符串foo
?>
< script>
var a ='<?php echo $ a; ?>; //在JS
的上下文中输出字符串foo //必须用引号括起来,以便当JS执行
时//它仍然是字符串foo //当浏览器执行此操作时,PHP将已经完成所有处理并退出
< / script>
<?php
//用$ a
做别的事// // JS现在还没有执行
?>

正如我所说的,在这种情况下,PHP(全部)在服务器上执行FIRST :


  1. 一个PHP变量 $ a 被创建为字符串'foo'

  2. $ a 的值在某些JavaScript(目前未执行)的环境中输出
  3. $ b $使用PHP的 $ a
  4. 完成更多的b
  5. 操作,包括具有var赋值的JS,将被发送到浏览器。

正如所写的,这导致以下内容被发送到浏览器执行(为了清晰起见,我删除了JS注释):

 < script> 
var a ='foo';
< / script>

然后,只有这样,JS才会开始执行它自己的变量一个设置为foo(在这一点PHP不在图片中)。



换句话说,如果两个人住在相同的文档并没有与服务器进行额外的交互,JS不能在PHP中产生任何效果。此外,PHP对JS的影响仅限于在JS上下文中输出一些JS或某些东西的简单能力。


I have the following code in JavaScript:

<script>
var a="Hello";
</script>

PHP Code:-

<?php 
$variable = // I want the above JavaScript variable 'a' value to be stored here
?>

Note : I don't want it in a form submission. I have some logic in JavaScript and I want to use it in the same PHP page... Please let me know how I can do this.

解决方案

You have to remember that if JS and PHP live in the same document, the PHP will be executed first (at the server) and the JS will be executed second (at the browser)--and the two will NEVER interact (excepting where you output JS with PHP, which is not really an interaction between the two engines).

With that in mind, the closest you could come is to use a PHP variable in your JS:

<?php
$a = 'foo'; // $a now holds PHP string foo
?>
<script>
    var a = '<?php echo $a; ?>'; //outputting string foo in context of JS
                                 //must wrap in quotes so that it is still string foo when JS does execute
                                 //when this DOES execute in the browser, PHP will have already completed all processing and exited
</script>
<?php
//do something else with $a
//JS still hasn't executed at this point
?>

As I stated, in this scenario the PHP (ALL of it) executes FIRST at the server, causing:

  1. a PHP variable $a to be created as string 'foo'
  2. the value of $a to be outputted in context of some JavaScript (which is not currently executing)
  3. more done with PHP's $a
  4. all output, including the JS with the var assignment, is sent to the browser.

As written, this results in the following being sent to the browser for execution (I removed the JS comments for clarity):

<script>
    var a = 'foo';
</script>

Then, and only then, will the JS start executing with its own variable a set to "foo" (at which point PHP is out of the picture).

In other words, if the two live in the same document and no extra interaction with the server is performed, JS can NOT cause any effect in PHP. Furthermore, PHP is limited in its effect on JS to the simple ability to output some JS or something in context of JS.

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

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