4

This is my php code that gets a list of files in a dir and stores it in the array $files: (Note I tried running this in a fiddle here but not sure if I am able with this example)

<?php

echo "<hr>";
echo "<hr>";


$files = array_slice(scandir('/path/to/dir'), 2); // this gets a list of all files in the dir

//
echo "this is the array: <br>";
print_r($files); // this prints the array 
echo "<br>";
echo "<br>";

echo "this is each element in the array: <br>";
foreach ($files as &$value) {
    print_r($value."<br>"); // this prints each element of the array 
}

echo "<hr>";
echo "<hr>";
?>

Immediately after that is my javascript where I want to store the php array in my javascript variable, but I get this error Uncaught SyntaxError: Unexpected token ILLEGAL in the console. Can anyone correnct the erro in my ways?

<script>
// then echo it into the js/html stream
// and assign to a js variable
var jsfiles =[];
jsfiles = "<?php print_r($files);?>";

// then
alert(jsfiles);
console.log("the files are:",jsfiles);

</script>

EDIT2 tried jsfiles = "<?php json_encode($files);?>"; which gives me no error but values in array are not displayed in console.log("the files are:",jsfiles);


EDIT3 - got it working

snippet of my code. I had to delete the 2 lines below, even though I though they were commented out. Not sure why

<script>
// then echo it into the js/html stream
// and assign to a js variable
//var jsfiles =[];
//jsfiles = "<?php print_r($files);?>";   ------ had to delete this line
//jsfiles = <?php json_encode($files) ?>; ------ had to delete this line
var jsfiles = <?php echo json_encode($files) ?>;
4
  • hint: json_encode Commented Jun 9, 2016 at 3:15
  • Do you see the alert? What is the output from the PHP? (ie view-source in your browser) Commented Jun 9, 2016 at 3:23
  • @Zadaz No dont see the alert, so it is not getting that far? it looks like this jsfiles = "Array ( [0] => data.tsv ... with an x near Array, what am I missing? Commented Jun 9, 2016 at 3:30
  • You had to delete the print_r line because it would have printed out multiple lines into your JavaScript code. The // would have only commented out the first line of the print_r output, the rest of it would have caused syntax errors. Commented Jun 9, 2016 at 4:26

2 Answers 2

5

Always always always use json_encode when outputting PHP to javascript.

var jsfiles = <?php echo json_encode($files) ?>;

Even if this were just a number or simple string, json_encode protects you from surprises and ensures it will be valid javascript object notation.

Sign up to request clarification or add additional context in comments.

6 Comments

@HattrickNZ make sure you have the echo part in there, I updated my answer
still does not work, get Uncaught SyntaxError: Unexpected number
@HattrickNZ then you have a different problem in your code. You aren't getting unexpected number from the json_encode output.
@HattrickNZ use view source to see what you're getting! look at the line numbers of your javascript error messages! do some basic troubleshooting here!
@HattrickNZ that's because you only commented out the JS code, your PHP print_r was still executing, spitting out all kinds of stuff inside your <script> tags and causing errors. Glad you got it working!
|
-1

You need to make sure that your output is in pure JSON format so that your javascript can easily parse it to use it.

You need to convert is to json: as jszobody suggested you can use json_encode(); to do that, but you are also required to set headers by...

header('Content-type:application/json;');

By this way javascript will easily parse the output to an javascript json object and you will be able to use it easily...

One more tip in final production stage, also put..

error_reporting(0); 

So only pure JSON output goes to javascript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.