Reputation: 27
i have a text file named "vars.txt" that holds an array. How can i pull that information and put it in a javascript array? right now i have
<script type="text/javascript">
function test() {
var testvar = <?php file_get_contents('vars.txt') ?>;
alert ("success");
alert (testvar);
};
</script>
and that is not working. is there a better way to pull this data into an array?
Upvotes: 0
Views: 113
Reputation:
Use json_decode()
to decode the JSON data from vars.txt
:
var testvar = <?php echo json_decode(file_get_contents('vars.txt')) ?>;
Upvotes: 0
Reputation: 11972
<script type="text/javascript">
function test() {
var testvar = <?php echo file_get_contents('vars.txt') ?>;
alert ("success");
alert (testvar);
};
</script>
You forgot to echo
the data, without this nothing will be rendered into the javascript function.
To debug situations like this, just view the source of the rendered webpage, and see what's actually printed.
Upvotes: 1