从外部JS读取PHP文件变量 [英] Reading a PHP file variables from external JS

查看:153
本文介绍了从外部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

这篇关于从外部JS读取PHP文件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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