Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Possible Duplicate:
pass php array to javascript function
Pass PHP array into Javascript array

I got problem pass the PHP data to Javascript. I need to transfer the addr column to array then pass array to Javascript , how can i fetch it out of PHP then use array in Javascript? thank you

<?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]; 
  } 

  ?>
share|improve this question

marked as duplicate by Corbin, sberry, Wesley Murch, Jan Hančič, Octavian Damiean May 8 '12 at 6:36

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Is this an API to which the js connects to, to get data, or does this php script output a page with javascript? –  Ahatius May 8 '12 at 6:36
    
no, this my PHP code, I wish people can help me out to get the var from it. :-) –  Shuyou Chang May 8 '12 at 6:38
    
Question was closed. I didn't ask if it's your code, I just wanted to know which way you want the data. AJAX call to php script or does the php script output the complete html code including javascript. –  Ahatius May 8 '12 at 6:42
    
Probably you will need an ajax call to get the array from php –  Moyed Ansari May 8 '12 at 6:46
    
I am so sorry I was misunderstanding. I need to fetch the Address data from database then pass it to my Geocoder for google map. I don't know which way is the best way to do it. thanks for your asking. –  Shuyou Chang May 8 '12 at 6:51

1 Answer 1

Try:

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.