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.
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];?>", "");
Your code twisted to where PHP is in charge and Javascript is just the echo tool. PHP does all the loop control and Javascript takes the dynamic variable through a PHP insert.
<!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 type="text/javascript">
prompt(<php? echo $files[i] ?>, "");
</script>
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.