I am describing here a very useful method for getting the values of a PHP array in JavaScript.
For this we need to assign the PHP array values to a array in JavaScript.
<?php $a = array('A','B','C'); ?> <html> <head> <title>Assign PHP Array to Javascript Array</title> <script language="javascript"> function showValues(){ var a=new Array; <?php for($i=0;$i<count($a); $i++) { echo "a[$i]='".$a[$i]."';\n"; } ?> for(i=0;i<a.length;i++) alert(a[i]); } </script> </head> <body onload="showValues()"> </body> </html>
Code is not complete, please share the complete code
it worked for me…
thanks.
Why does your code ‘converting array from php to javascript’ not work (at least not show the values) when instead of array (‘A’,’B’,..) I fill the array with a file on the server: $a=file(‘tekst1.txt’) ?
Can you paste your code here, we cannot use loop for files
json is better. see bellow
Assign PHP Array to Javascript Array
function showValues(){
var a=
}
Code seems to be missing
you absolute ripper! helped me out a treat! what a ledge!! cheers
A simple solution. Works perfect!!
Thanks
its working, thanks
var jsarray = ;
may be JSON is better choose.