Passing an Array from PHP to Javascript -


i'm trying pass on php array use array in javascript.

the php code i'm using follows:

<?php  $link = mysqli_connect("localhost", "root", "password", "database");      /* check connection */     if (mysqli_connect_errno()) {         printf("connect failed: %s\n", mysqli_connect_error());         exit();     }      $query = "select * employees";      if ($result = mysqli_query($link, $query)) {          /* fetch associative array */         while ($row = mysqli_fetch_assoc($result)) {              $data[] = $row;         }         print_r($row);         /* free result set */         mysqli_free_result($result);     }     /* close connection */     mysqli_close($link);  //convert php array json format, works javascript $json_array = json_encode($data); ?> 

javascript:

<script>      var array = <?php echo $data; ?>;      console.log(array);  </script> 

the data array in php doesn't seem passed on javascript var array. when looking @ console on firebug following error messages displayed:

notice - array string conversion.

i'd appreciate why error occurring.

i believe should be:

<script> var array = <?php echo $json_array; ?>; console.log(array); </script> 

you're using json_encode in $data you're not using variable in code. it?


Popular posts from this blog