Insert MySQL data into JavaScript with PHP

I found this really nice JS Tree script,,
and I want to populate the tree with data from
MySQL but can’t get it to work.

I tried ob_start() without any luck
and a wrapper:

<body bgcolor=“#FFFFFF”>
<h1>JS Tree</h1>
<script language=“JavaScript” type=“text/javascript” src=“js.php”></script>


header(“Content-Type: text/javascript”);

connect to mysql
make query


  document.write('JS Tree');      //Working

  d = new dTree('d');

  d.add(0,-1,'My example tree');
  d.add(1,0,'&lt;?php echo $mysqlfield;?&gt;','example01.html');
  d.add(2,0,'Node 2','example01.html');


but it don’t work. It doesn’t output the tree at all,
only document.write(‘JS Tree’);


Try to just print out variables containing results from MySQL query, that will help you to get one step closer to finding the problem.

All you need to do is echo the MySQL data into your JS code at the relevant points. PHP runs on the server and the output is (mainly) just text data as far as PHP is concerned - it doesn’t know or care whether this data is HTML tags, JavaScript code, plain text.


<script type="text/javascript">
d = new dTree ('d');
d.add (0,-1,'My example tree');

// Connect to MySQL and get data, then
while ( $row = mysql_fetch_array ($rs) ) {
    $p1 = $row['Index'];
    $p2 = $row['Level'];
    $p3 = $row['URL'];
    echo "d.add ($p1,$p2,'$p3');" . "\

document.write (d)


So simple.