Skip to content
Advertisement

Calling a php variable in JS does not work

I’m trying to make a user input by reading the contents of my server directory with php. So the user can select a file, in my case a CSV file which gets saved into a variable and then gets processed further down in my JavaScript code that should make a chart from it. All it does now is output the path string of the selected file. I tried using the json_encode function but it still doesn’t seem to work.

JavaScript

Advertisement

Answer

If you assign a String to a variable in JavaScript, use " or ':

var x = "Hello";

Your current code results in

var fileName = <?php echo json_encode($graphen); ?>; => var fileName = variable;

Add quotes and it should work:

var fileName = '<?php echo json_encode($graphen); ?>'; => var fileName = 'variable';

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement