erere erere - 1 year ago 76
HTML Question

Looping through a PHP array in javascript

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>

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

function func() {

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



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:

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


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
for the php code in javascript but that doesn't seem to make a difference.

printing the array at a exact position like
works, but using a for loop doesn't.

Answer Source

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];?>", "");


var id = prompt("<?php for($i=2; $i<count($files);$i++)echo $files[$i];?>", "");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download