First of all about Json_encode GBK Chinese garbled, create a JSON function, this section from the online a warrior
/************************************************************** * * handles all elements in an array with a specific function * @param string &A MP $array the string to be processed * @param string $function the function to execute * @return Boolean $apply _to_keys_also Apply to Key * @access public**************************************************************/functionArrayrecursive (&$array,$function,$apply _to_keys_also=false){ foreach($array as $key=$value) { if(Is_array($value) {arrayrecursive ($array[$key],$function,$apply _to_keys_also); } Else { $array[$key] =$function($value); } if($apply _to_keys_also&&is_string($key)) { $new _key=$function($key); if($new _key!=$key) { $array[$new _key] =$array[$key]; unset($array[$key]); } } }}/************************************************************** * * Converts an array to a JSON string (Chinese-compatible) * @param array $array The array to convert * @return the JSON string converted by String * @access public*************************************************** ***********/functionJSON ($array) {arrayrecursive ($array, ' UrlEncode ',true); $json= Json_encode ($array); return UrlDecode($json);}
Connect to the database to value the array $array1
$DBCNX= @mysql_connect("localhost", "root", "1234" );if(!$DBCNX) { Echo("Unable to connect to the".) Database server at this time. "); Exit ();}if(! @mysql_select_db("PMS" )) { Echo("Unable to locate the joke".) Database at this time. "); Exit ();}mysql_query("SET NAMES ' GB2312 '" ); $q=mysql_query("SELECT * from ability where ALV = 1"); while($row=Mysql_fetch_array($q)){ $array 1[] =$row[AName];}
Array array1 passed to JavaScript to the array ability1
<script type= "Text/javascript" src= "js/jquery-1.7.2.min.js" ></script><script type= "Text/javascript ">var ability1=<?php Echo JSON ($array 1);? >; var a=eval ("ability1"); alert (a[0]); </script>
PHP arrays are passed to JavaScript and json_encode to solve the GBK Chinese garbled