Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中访问存储在Php数组中的数据_Php_Javascript_Arrays_File Read - Fatal编程技术网

在javascript中访问存储在Php数组中的数据

在javascript中访问存储在Php数组中的数据,php,javascript,arrays,file-read,Php,Javascript,Arrays,File Read,可能重复: 我正在用php在服务器中读取一个文件,并将文件内容存储在一个变量中。现在我想用javascript访问变量,因为我需要用制表符分隔内容,并将内容作为选项添加到Select标记中 <?php //read the 'file' content to lines variable $lines = file('file'); ?

可能重复:

我正在用php在服务器中读取一个文件,并将文件内容存储在一个变量中。现在我想用javascript访问变量,因为我需要用制表符分隔内容,并将内容作为选项添加到Select标记中

               <?php
                       //read the 'file' content to lines variable
                       $lines = file('file');
                ?>

访问PHP变量的Javascript($line):


函数readData(){
var s='';
警报;
}
其中,警报仅与数组一起弹出文本


我应该如何做才能在javascript数组变量中访问存储在$lines数组中的数据?函数是使用数组中的get file contents,然后逐行迭代(比如在foreach中)。例如:

foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}

如果在JS中使用内容,则必须注意引号等特殊字符。

file\u get\u contents('file')
而不是
file('file'))
是你最好的选择。

可能是一个开始的地方,但也不清楚JavaScript何时以及如何执行。你是如何被允许问这个常见问题的?@Shamim在HTML按钮点击事件中调用Js函数的
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;