Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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) ?>;
share|improve this question
    
hint: json_encode – Ghost Jun 9 at 3:15
    
Do you see the alert? What is the output from the PHP? (ie view-source in your browser) – dtkaias Jun 9 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? – HattrickNZ Jun 9 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. – dtkaias Jun 9 at 4:26
up vote 1 down vote accepted

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.

share|improve this answer
    
did not work, do i need quotes? – HattrickNZ Jun 9 at 3:33
    
@HattrickNZ make sure you have the echo part in there, I updated my answer – jszobody Jun 9 at 3:37
    
still does not work, get Uncaught SyntaxError: Unexpected number – HattrickNZ Jun 9 at 3:42
1  
@HattrickNZ then you have a different problem in your code. You aren't getting unexpected number from the json_encode output. – jszobody Jun 9 at 3:44
1  
@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! – jszobody Jun 9 at 11:53

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.