简体   繁体   English

从JavaScript循环变量访问PHP变量

[英]Access PHP variable from JavaScript loop variable

I am creating variables from loops in PHP and JavaScript using the same names. 我正在使用相同的名称通过PHP和JavaScript中的循环创建变量。 These names are stored in arrays (one in PHP and the other one in JS). 这些名称存储在数组中(一个在PHP中,另一个在JS中)。 Each variable in PHP has information that I need to pass in JavaScript. PHP中的每个变量都有我需要传入JavaScript的信息。 Now, this is working: 现在,这正在工作:

vars_jsarray =["Name1", "Name2", "Name3"];

for (var i = 0; i < vars_jsarray.length; i++){
  this[vars_jsarray[i]] = [<?=${$vars_phparray[0]};?>]; 
}

I want to change that "0" and use variable "i" instead. 我想更改为“ 0”,而是使用变量“ i”。 Something like this could work too (because the arrays are the same): 这样的事情也可以工作(因为数组是相同的):

vars_jsarray =["Name1", "Name2", "Name3"];

for (var i = 0; i < vars_jsarray.length; i++){
  this[vars_jsarray[i]] = [<?=${vars_jsarray[i]};?>]; 
}

How could I access to PHP variable using this JavaScript loop? 如何使用此JavaScript循环访问PHP变量?

Assuming the arrays are exactly the same, drop the JS loop and use a PHP one: 假设数组完全相同,则删除JS循环并使用一个PHP循环:

<?php
  for ($i = 0; $i < count($vars_phparray); $i++){
    echo "this[ {$vars_phparray[$i]} ] = [ {$vars_phparray[$i]} ];"; 
  }
?>

Although in future you may find that this way of working gets very messy, very quickly . 尽管将来您可能会发现这种工作方式变得非常混乱,非常迅速

Instead you could use some AJAX to grab data from your server as it described here . 相反,您可以使用一些AJAX从服务器中获取数据, 如此处所述

Off the top of my head 从我的头顶

vars_jsarray =["Name1", "Name2", "Name3"];
vars_js_phparray = [<?=implode("," $vars_phparray);?>]

for (var i = 0; i < vars_jsarray.length; i++){
  this[vars_jsarray[i]] = [vars_js_phparray[i]]; 
}

Not sure if that solves your requirement but based on your example it is an exact replica. 不确定是否可以满足您的要求,但是根据您的示例,它是一个完全相同的副本。

You are misunderstanding how PHP works; 您会误解PHP的工作方式; this is most certainly not going to work - not in its current form. 这肯定是行不通的-不是目前的形式。

In general, PHP will first produce a HTML document which is then sent to the client (browser). 通常,PHP首先会生成一个HTML文档, 然后将其发送到客户端(浏览器)。 At this point the PHP script is finished with executing your server side code*. 至此,PHP脚本已完成执行服务器端代码*。

The produced document can of course contain JavaScript, which will run on the client after the actual page has been sent to the client. 生成的文档当然可以包含JavaScript,它将在实际页面发送到客户端在客户端上运行。 There is no back-and-forth communication, not unless you use AJAX calls, but that is much more well structured than plain language-mixing. 没有来回通信,除非您使用AJAX调用,否则这种通信的结构比纯语言混合要好得多。

That is, in this case you basically write JavaScript to the client's browser using PHP, which will be ran on the client. 也就是说,在这种情况下,您基本上是使用PHP在客户端的浏览器中编写JavaScript,而该PHP将在客户端上运行。 Consider the following example: 考虑以下示例:

PHP code PHP代码

echo "<script>";

for ($i = 0; $i < 3; $i++)
{
    echo "console.log('Let us write something into console: $i');";
}

echo "</script>";

This code will send the following page to the client: 这段代码会将以下页面发送给客户端:

HTML/JavaScript HTML / JavaScript

<script>
    console.log('Let us write something into console: 0');
    console.log('Let us write something into console: 1');
    console.log('Let us write something into console: 2');
</script>

When the above very simple Javascript is executed, there is no way of accessing PHP's $i variable that was used to create it, since it no longer exists. 当执行上述非常简单的Javascript时,由于它不再存在,因此无法访问用于创建PHP的$i变量。 Certainly not on the client's machine. 当然不在客户端计算机上。


*assuming an output buffer is used, otherwise the page is sent as it is created, line-by-line. *假设使用了输出缓冲区,否则页面将在创建时逐行发送。

You can access PHP variables within JS accordingly: 您可以相应地在JS中访问PHP变量:

var jsVar = <?php echo $phpVar; ?>;

But, again, I don't recommend it. 但是,再次,我不建议这样做。 Use AJAX. 使用AJAX。

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

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