2012-09-11 46 views
0

發送特殊字符(即卡隆)通過xmlhttp.responseText我試圖從數據庫中的特殊字符(卡隆)抓取數據,並通過使用xmlhttp.responseText到json_encode填充文本框發送。與包含特殊字符(caron)的數據關聯的特定文本框不顯示任何內容。其他文本框顯示正確的數據。我嘗試使用Javascript函數encodeURIComponent,但只在文本框中顯示null。任何幫助,將不勝感激。使用json_encode

主頁代碼:

function loadDoc() 
{ 
    var xmlhttp; 

    // code for IE7+, Firefox, Chrome, Opera, Safari 
    if (window.XMLHttpRequest) 
    { 
     xmlhttp=new XMLHttpRequest(); 
    } 
    // code for IE6, IE5 
    else 
    { 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.onreadystatechange=function() 
    { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
     var a = JSON.parse(xmlhttp.responseText); 
     document.getElementById("textbox").value=a.first; 
     document.getElementById("textbox2").value=a.second; 
     document.getElementById("textbox3").value=a.third; 
     document.getElementById("textbox4").value=a.fourth; 
     document.getElementById("textbox5").value=a.fifth; 
     document.getElementById("textbox6").value=a.sixth; 
     } 
    } 

    xmlhttp.open("GET","loadTextBox.php?id=4",true); 
    xmlhttp.send(); 
} 

loadTextBox.php代碼:

<?php 
header("Content-type: application/json"); 

---Placeholder for correct DB login info--- 

$result = $mysql->query(---Placeholder for correct SQL query---); 

while ($row = $result->fetch_object()) 
{ 
    $queryResult[] = $row->colun_one; 
} 
$textboxValue = $queryResult[0]; 
$textboxValue2 = $queryResult[1]; 
$textboxValue3 = $queryResult[2]; 
$textboxValue4 = $queryResult[3]; 
$textboxValue5 = $queryResult[4]; 
$textboxValue6 = $queryResult[5]; 
echo  
json_encode(array('first'=>$textboxValue,'second'=>$textboxValue2, 
'third'=>$textboxValue3,'fourth'=>$textboxValue4,'fifth'=>$textboxValue5, 
'sixth'=>$textboxValue6)); 
?> 

回答

0

添加在線路$ mysql->查詢( 「SET字符集 'UTF8'」);連接到數據庫之後,在SQL查詢修復之前loadTextBox.php內部。

0

使用UTF-8發送之前進行編碼。

utf8_encode($variable);

或嘗試array_map();到陣列編碼;

$textboxValue = $queryResult[0]; 
$textboxValue2 = $queryResult[1]; 
$textboxValue3 = $queryResult[2]; 
$textboxValue4 = $queryResult[3]; 
$textboxValue5 = $queryResult[4]; 
$textboxValue6 = $queryResult[5]; 
$arrayToEncode = array('first'=>$textboxValue,'second'=>$textboxValue2, 
'third'=>$textboxValue3,'fourth'=>$textboxValue4,'fifth'=>$textboxValue5, 
'sixth'=>$textboxValue6); 
$encodedArray = array_map('utf8_encode', $arrayToEncode); 
echo json_encode($encodedArray); 

您可能還需要設置你的xmlhttp.requestutf-8以及

xmlhttp.setRequestHeader("Content-Type", "text/plain;charset=UTF-8"); 
xmlhttp.open("GET","loadTextBox.php?id=4",true); 
xmlhttp.send(); 

您可能需要嘗試一種或另一種或甚至兩者。您還想確認您的數據庫是否能夠存儲特殊字符,如果數據庫沒有存儲特殊字符,那麼您的應用程序將無法讓它們對其進行編碼。

+0

你能提供代碼示例嗎? – programm3r

+0

@ programm3r更新答案 – Bot

+0

array_map使它如此,在它被顯示(同樣的事情encodeURIComponent函數一樣)具有卡隆每一個字的信,但有它上面的卡隆還在信中沒有得到顯示。使用setRequestHeader不會顯示任何內容。我可以在我的DB中看到caron,所以我假設DB能夠痛苦特殊角色。 – programm3r