[RESOLVED] PHP array to Javascript array undefined
Hello all,
my problem is getting the value from PHP array I get from MySQL query into the Javascript array, which says it is UNDEFINED.
If I use this code, everything is ok:
PHP Code:
<?php
$n = array('test','test2','test3');
print_r($n);
echo '<br>';
echo '<script type="text/javascript">';
echo 'var List = new Array("', join($n,'","'), '");';
echo '</script>';
?>
<script type="text/javascript" language="javascript">
document.write(List);
</script>
Output is:
PHP: Array ( [0] => test [1] => test2 [2] => test3 )
Javascript: test,test2,test3
The problem is with the array I get from Database:
PHP Code:
<?php
$con = mysql_connect('databse', 'user', 'password');
mysql_select_db("database_name", $con);
$query="Select field from table";
$result=mysql_query($query,$con);
$rows=mysql_num_rows($result);
$n = array();
for($i=0;$i<$rows;$i++)
{
$data=mysql_fetch_row($result);
$n[] = $data[0];
}
print_r($n);
echo '<br>';
echo '<script type="text/javascript">';
echo 'var List = new array("', join($n,'","'), '");';
echo '</script>';
?>
<script type="text/javascript" language="javascript">
document.write(List);
</script>
Output is:
PHP: Array ( [0] => test [1] => test2 [2] => test3 )
Javascript: undefined
I'm a bit confused why is that, so help would be much appreciated.
Thnx.