Hi,
someone kindly provided some script to show how to populate a javascript array with data from a mySQL dataset but I'm still not understanding what's happening. E.g. in the following example I end up with 'undefined' when I try to document.write the array elements.
Is the method I'm using here to load the javascript array correct in principle?
Can anyone say why this isn't working?
<head>
<script language="javascript">
var myarr = new Array();
</script>
</head>
<body>
<?php
// link to database and get data
// ...
// create variables for loading JS array
//
$jstr1 = "<script language='javascript'> <br /> myarr[";
$jstr3 = "// -->";
$jstr4 = "</script";
for ($i = 0; $row=mysql_fetch_row($result); $i++)
{
$jstr2 = $jst1 . $i . "]=" . $row[2] . ";" ;
echo $jstr2;
echo $jstr3;
echo $jstr4;
}
?>
<script language='javascript'>
document.write(myarr[3]);
</script>
</body>