Looping through a PHP array in javascript

erere

I've made an array that has all the file names in a certain directory. I've tested it and printed it to the screen. I want to use that array in javascript code, print it's members to the screen as well for a php for loop.

<!DOCTYPE html>
<html>
<head>
<body>

<?php
$i=0;
$files = scandir('uploads');
for($i=2; $i<count($files);$i++) {
    echo '<br>';
    print_r( $files[$i]);
}
?>

<script>
function func() {

    var id = prompt("<?php for($i=2; $i<count($files);$i++)echo $files[i];?>", "");

}
</script>

</body>
</html>

This loops through the array $files and prints it to the screen, I want to loop through the same array in a window.prompt object in javascript, and print the list to the prompt popup.

Before the end of the body tag:

<script>
function func() {
    var id = prompt("<?php for($i=2; $i<count($files);$i++)echo $files[i];?>", "");

}
</script>

But this seems to not print any of the array to the prompt object.

However if I do:

var list = prompt("<?php echo $files[2];?>", "");

It actually prints an element of the array, without the loop. But I want to print all at once, how can I accomplish this?

I tried using print_r() for the php code in javascript but that doesn't seem to make a difference.

printing the array at a exact position like $file[4] works, but using a for loop doesn't.

Ultrazz008

Change i to $i, to be closer: $files[i] should be $files[$i]

var id = prompt("<?php for($i=2; $i<count($files);$i++)echo $files[i];?>", "");

use:

var id = prompt("<?php for($i=2; $i<count($files);$i++)echo $files[$i];?>", "");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related