I’ve been searching for this a while now, and though my problem seems a lot easier than comparable problems others are posting, I still can’t find a solution.
I would like an array to fill with content from a .txt file. The thing is (opposed to other people having this problem), I don’t have to do anything with the contents from the txt file, since it’s already in the form of [1, 2, 3], [a, b, c], ... etc. So the only thing I’d like to do is print content from a txt file in
Somewhere between <script> and </script>, there is:
var locations = [
*contents of the txt file should go here*
];
Thats it! Is there a way to grab the contents from a txt file and print them in javascript?
First of all, excuse me for not describing the problem in good detail. I’ve found the solution to my problem and I’m fairly new here (and not that good of a programmer), but I’ll try to explain the answers.
First, the problem:
I have php page, on which I have some javascript code in the
<head>section. In that javascript I want to load the contents from a txt file. The exact text which is in the txt file is:I know this isn’t exactly a beautiful solution and I know that the
var locations [];part should be in the javascript instead of in the txt file, but this works fine for me.Solution:
In the javascript I’ve put
<?php echo file_get_contents('text.txt'); ?>where I want the content of the textfile to show up and that’s it!The reason it solves my problem is because I have to update the array of locations frequently, and the code appears on about 30 pages. I didn’t want to have to update all the
<head>sections of those pages when there are new locations, so I wanted to put some code in the javascript that grabs the locations from an external file. Now I only have to update one file.Again, I realize I wasn’t describing my problem in a clear way, but I hope that others having the same problem can use this solution!