How To Open File And Load Contents Into A Variable

Hi,

in my PHP webpage I specify 3 files, like:


$first_file = $_POST["one"];
$second_file = $_POST["two"];
$third_file = $_POST["three"];

then I have a JS fuction, that in some points, needs to open each file and store the data of each file into a variable. My questions are 2:
First, how do I save all data of each text file into a JS variable?
Second, the part of the Js function which has these variables looks like this:


              series:
                [
                    {
                        name: 'ONE',
                        color: 'blue',
                        data:
                        [
                                 HERE WE SHOULD PLACE ALL DATA FROM FILE 1
                        ]
                    },

                    {
                        name: 'TWO',
                        color: 'yellow',
                        data:
                        [
                                 HERE WE SHOULD PLACE ALL DATA FROM FILE 2
                        ]
                    },

                    {
                        name: 'THREE',
                        color: 'red',
                        data:
                        [
                                    HERE WE SHOULD PLACE ALL DATA FROM FILE 3
                        ]
                    }
         ]

Can you please help me, how I can put the data of each file into the correct place? I am new to JS and came across a helpful snipet of code, but I do not know how I can place my data inside the JS code so as to use them.

Thanks a lot!

Hi ktsirig,

Currently there is no clear/defined way to simply just inject things directly into JavaScript, however on that same note you can take advantage of your PHP code as pre-load the file then JSON encode the contents which can then be passed to the JavaScript code. See the below example which is loosely based of your above snippet.

include('path/to/the/file.php');

// Lets say the contents of the file is
// 
// $myArray = array('some value', array('another value', 'something else'), 'another thing');

// Echo out the contents by wrapping them inside a <script> tag
echo '<script type="text/javascript">
    var someFileContents = ' . json_encode($myArray) . '
</script>';
var series = [{data : someFileContents}];

Of course that is just an example and it only assumes you are using the same type of code structure.