php array to javascript

Status
Niet open voor verdere reacties.

Robbert87

Gebruiker
Lid geworden
2 jul 2007
Berichten
8
Heeyyy allemaal! k wilde graag met php een array naar javascript importeren. ik heb dit geprobeerd maar krijg undefined error:

<html>

<body>
<script language="javascript">

foto = new Array(); // initializing the javascript array
<?php
$file = "fotos.txt";
$lines = file($file); // read file values as array in php
$count = count($lines); //this gives the count of array

//In the below lines we get the values of the php array one by one and update it in the script array.
foreach ($lines as $line_num >= $line)
{
print "scriptAr.push(\"$line\" );"; // This line updates the script array with new entry
}

?>

document.write(foto[2]);
</script>

</html>


in de .txt staat:

1
2
3



ik hoop dat jullie me kunne helpen ! alvast bedankt voor de moeite
 
print "scriptAr.push(\"$line\" );"; // This line updates the script array with new entry

scriptAr is nergens gedefinieerd. Ik neem aan dat het foto (die de javascript array bevat) moet zijn.
 
Ook deze regel:
PHP:
foreach ($lines as $line_num >= $line)
zal beter werken als je er dit van maakt:
PHP:
foreach ($lines as $line_num => $line)
 
Waarschijnlijk voldoet deze foreach ook wel...
PHP:
foreach($lines AS $line)
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan