想把PHP的結果集直接傳遞到JavaScript中。PHP從數據庫中取數據,數據庫為GBK編碼,PHP和JavaScript所在的html都采用utf-8編碼。
下面是html文件:
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
< title >測試PHP和JavaScript交換參數 </ title >
</ head >
< body >
< script type ="text/javascript" >
function getProfile(str)
{
var arr = eval(str); // 或eval(str)
var s = "" ;
for ( var i = 0 ; i < arr.length; i ++ )
{
s += " name: " + arr[i].name + " ,nick: " + arr[i].nick + " : " ;
}
document.getElementById( ' TestName ' ).innerHTML = s;
}
</ script >
< body >< div id ="TestName" ></ div ></ body >
< script type ="text/javascript" src ="../TestJS.php" ></ script >
</ body >
</ html >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=utf-8" />
< title >測試PHP和JavaScript交換參數 </ title >
</ head >
< body >
< script type ="text/javascript" >
function getProfile(str)
{
var arr = eval(str); // 或eval(str)
var s = "" ;
for ( var i = 0 ; i < arr.length; i ++ )
{
s += " name: " + arr[i].name + " ,nick: " + arr[i].nick + " : " ;
}
document.getElementById( ' TestName ' ).innerHTML = s;
}
</ script >
< body >< div id ="TestName" ></ div ></ body >
< script type ="text/javascript" src ="../TestJS.php" ></ script >
</ body >
</ html >
下面是PHP文件:
<?php
include_once("./class.config.php");
include_once("include/DB.class.php");
// PHP數組傳遞給JavaScript以及json_encode的gbk中文亂碼的解決
/* *************************************************************
*
* 使用特定function對數組中所有元素做處理
* @param string &$array 要處理的字符串
* @param string $function 要執行的函數
* @return boolean $apply_to_keys_also 是否也應用到key上
* @access public
*
************************************************************ */
function arrayRecursive(& $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]);
}
}
}
}
/* *************************************************************
*
* 將數組轉換為JSON字符串(兼容中文)
* @param array $array 要轉換的數組
* @return string 轉換得到的json字符串
* @access public
*
************************************************************ */
function JSON( $array) {
arrayRecursive( $array, 'urlencode', true);
$json = json_encode( $array);
return urldecode( $json);
}
$test = $db->Select("test");
// 構建JSON串
$jstr='[';
while( $rs = $test->Fetch())
{
$nick = iconv("GBK",'utf-8', $rs['nick']); /* 轉換為utf-8編碼 */
$jstr= $jstr.'{"name":"'. $rs['name'].'","nick":"'. $nick.'"},';
}
$jstr= substr( $jstr,0, strlen( $jstr)-1);
$jstr= $jstr.']';
echo "getProfile(' $jstr')";
?>
include_once("./class.config.php");
include_once("include/DB.class.php");
// PHP數組傳遞給JavaScript以及json_encode的gbk中文亂碼的解決
/* *************************************************************
*
* 使用特定function對數組中所有元素做處理
* @param string &$array 要處理的字符串
* @param string $function 要執行的函數
* @return boolean $apply_to_keys_also 是否也應用到key上
* @access public
*
************************************************************ */
function arrayRecursive(& $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]);
}
}
}
}
/* *************************************************************
*
* 將數組轉換為JSON字符串(兼容中文)
* @param array $array 要轉換的數組
* @return string 轉換得到的json字符串
* @access public
*
************************************************************ */
function JSON( $array) {
arrayRecursive( $array, 'urlencode', true);
$json = json_encode( $array);
return urldecode( $json);
}
$test = $db->Select("test");
// 構建JSON串
$jstr='[';
while( $rs = $test->Fetch())
{
$nick = iconv("GBK",'utf-8', $rs['nick']); /* 轉換為utf-8編碼 */
$jstr= $jstr.'{"name":"'. $rs['name'].'","nick":"'. $nick.'"},';
}
$jstr= substr( $jstr,0, strlen( $jstr)-1);
$jstr= $jstr.']';
echo "getProfile(' $jstr')";
?>