[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 04:37 AM .
Reason: wrap the code [php][/php]
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}
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).
Originally Posted by
Dranve
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!
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 ( $result , MYSQL_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.
I'm getting better. Just put
PHP Code:
mysql_query ( "SET NAMES 'utf8'" );
under mysql_connect.
Once more, thnx for all your help.
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
Forum Rules
Bookmarks