如何将php数组传递给Javascript?

时间:2012-05-08 06:32:00

标签: php javascript mysql

  

可能重复:
  pass php array to javascript function
  Pass PHP array into Javascript array

我遇到问题将PHP数据传递给Javascript。我需要将addr列传递给数组然后将数组传递给Javascript,我如何从PHP中获取它然后在Javascript中使用数组?谢谢

<?php
  require_once 'Common/system_start.php';
  $sql="select addr from tab_mem order by sn desc ";
  $result=  mysql_query($sql);
  $data=mysql_fetch_assoc($result);
  $row=mysql_num_rows($result);
  $n = array(); 
  for($i=0;$i<$rows;$i++) 
  { 
   $data=mysql_fetch_row($result); 
   $n[i] = $data[i]; 
  } 

  ?>

1 个答案:

答案 0 :(得分:0)

尝试:

<script>
var jsArray = ["<?php echo join("\", \"", $n); ?>"];
alert(jsArray);
</script>