从外部JS读取PHP文件变量 [英] Reading a PHP file variables from external JS
问题描述
我如何通过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
这篇关于从外部JS读取PHP文件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!