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?
<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.
使用json_decode()
解码vars.txt
的JSON数据:
var testvar = <?php echo json_decode(file_get_contents('vars.txt')) ?>;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.