-1

Possible Duplicate:
Inserting PHP array into Javascript array

I'm reading a file in server with php and storing the file content in a variable. now i want to access the variable with javascript, Since I need to split the contents with tab delimited and add the content as options to a Select tag.

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

Javascript to access the PHP variable($lines):

<script type="text/javascript" >
    function readData(){
        var s = '<? echo $lines ?>';
        alert(s);
        }
</script>

Where alert pops up only with Array text

What Should I do so that the data stored in $lines array will be accessed in javascript array variable

Community
  • 1
  • 1
Emmi
  • 223
  • 1
  • 2
  • 8

2 Answers2

0

file function is to use get file contents in an array and then iterate (say in foreach) line by line.like:

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

You can use file_get_contents() to return the contents of a file as a string. like

$homepage = file_get_contents('http://www.example.com/');
echo $homepage;

if you are using contents in JS, you 'll have to take care of special characters like quotes.

Riz
  • 9,703
  • 8
  • 38
  • 54
0

file_get_contents('file') instead of file('file') is your best bet.

user1020317
  • 654
  • 5
  • 22