www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] PHP array to Javascript array undefined

Hybrid 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]

  2. #2
    Join Date
    Mar 2007
    Location
    Canada
    Posts
    99
    I'd use this function

    http://php.net/manual/en/function.json-encode.php

    PHP Code:
    Example #1 A json_encode() example
    <?php
    $arr 
    = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

    echo 
    json_encode($arr);
    ?>

    The above example will output:

    {"a":1,"b":2,"c":3,"d":4,"e":5}

  3. #3
    Join Date
    May 2011
    Posts
    4
    Thnx for the reply, but the server I'm working on is on PHP 5.0 which doesn't support json function and I cannot change that (not my server, hosting).

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Dranve View Post
    Thnx for the reply, but the server I'm working on is on PHP 5.0 which doesn't support json function.
    Who said so? Anyway, here's some alternatives, in case the json functions were not implemented in a certain version of PHP:
    http://tinsology.net/2011/04/php-jso...-alternatives/
    See also the comment below!

  5. #5
    Join Date
    May 2011
    Posts
    4
    Thank you very much for your answers, but I've found a quick and easy solution.
    PHP Code:
        <script>
        var list=[];
        <?php
          $db
    =mysql_connect('dbhost','username','password') or die('Error connecting to the server');
          
    mysql_select_db('mydatabase') or die('Error selecting database');
          
    $result=mysql_query('SELECT * FROM mytable') or die ('Error performing query');
          while(
    $row=mysql_fetch_array($resultMYSQL_ASSOC)){
        
    ?>
        list[list.length]=<?php echo '"'.$row['name'].'"'?>;
        <?php
          
    }
        
    ?>
        </script>
    I've got it from this site: http://www.devshed.com/c/a/PHP/PHP-a...ient-part-1/1/

    Now there is the problem of UTF-8 encoding 'cause my text is in Croatian and some symbols aren't showing properly. So, if someone knows the solution, that would be great. Thnx.

  6. #6
    Join Date
    May 2011
    Posts
    4
    I'm getting better. Just put

    PHP Code:
     mysql_query("SET NAMES 'utf8'"); 
    under mysql_connect.

    Once more, thnx for all your help.

  7. #7
    Join Date
    Mar 2007
    Location
    Canada
    Posts
    99

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