从外部JS读取PHP文件变量

问题描述:

我如何通过JS(我的意思是外部脚本)检索(php)包含文件中指定的某些变量的值,如下所示?

How may I retrieve, by JS (and I mean an external script), the value of some variables assigned in a (php) include file like the one below?

<?php 
$var1 = "a";
$var2 = "foo";
?>


为了重申之前的反馈,PHP用于生成HTML - PHP文件本身永远不可用于浏览器。您可以使用variables.php生成隐藏标签,然后使用JavaScript来读取它们。

To reiterate the previous feedback, PHP is used to generate HTML -- the PHP file itself is never available to the browser. You can use the variables.php to generate hidden tags, then JavaScript to read them.

例如,

variables.php输出:foo
javascript:document.getElementById('varA')。innerText

variables.php output: foo javascript: document.getElementById('varA').innerText

variables.php输出:
javascript:document.getElementById('varB')。value

variables.php output: javascript: document.getElementById('varB').value