www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] PHP array to Javascript array undefined

Threaded View

  1. #1
    Join Date
    May 2011
    Posts
    4

    resolved [RESOLVED] PHP array to Javascript array undefined

    Hello all,

    my problem is getting the value from PHP array I get from MySQL query into the Javascript array, which says it is UNDEFINED.

    If I use this code, everything is ok:
    PHP Code:
    <?php
    $n 
    = array('test','test2','test3');
    print_r($n);
    echo 
    '<br>';
     echo 
    '<script type="text/javascript">';
     echo 
    'var List = new Array("'join($n,'","'), '");';
     echo 
    '</script>';
    ?>

    <script type="text/javascript" language="javascript"> 
    document.write(List);
    </script>
    Output is:

    PHP: Array ( [0] => test [1] => test2 [2] => test3 )
    Javascript: test,test2,test3

    The problem is with the array I get from Database:
    PHP Code:
    <?php
    $con 
    mysql_connect('databse''user''password');
    mysql_select_db("database_name"$con);
    $query="Select field from table";
    $result=mysql_query($query,$con);
    $rows=mysql_num_rows($result);
    $n = array();
    for(
    $i=0;$i<$rows;$i++)
    {
    $data=mysql_fetch_row($result);
    $n[] = $data[0];
    }
    print_r($n);
    echo 
    '<br>';
    echo 
    '<script type="text/javascript">';
    echo 
    'var List = new array("'join($n,'","'), '");';
    echo 
    '</script>';
    ?> 

    <script type="text/javascript" language="javascript"> 
    document.write(List);
    </script>
    Output is:

    PHP: Array ( [0] => test [1] => test2 [2] => test3 )
    Javascript: undefined


    I'm a bit confused why is that, so help would be much appreciated.
    Thnx.
    Last edited by Kor; 05-23-2011 at 05:37 AM. Reason: wrap the code [php][/php]

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles