2012-12-16 36 views
0

我使用POST方法將一些值傳遞給了一個帶有ajax的php文件。ajax,php,mysql和charset的問題

當php文件接收到特殊字符如àèì時,它看到它正確,但是當我將它放入查詢中時,它們在數據庫上保存不好。

我在哪裏錯了?我試圖改變字符集到MySQL表...但沒有。

這是我的代碼。

阿賈克斯文件:

if (window.XMLHttpRequest) 
      { 
       xmlhttp=new XMLHttpRequest(); 
      } 
      else 
      { 
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        var response = xmlhttp.responseText; 
        if(response == "error") 
        { 
         post_status.innerHTML = "<font color=\"red\">Si è verificato un errore</font>"; 
        } 
        else if(response == "error_tofast") 
        { 
         post_status.innerHTML = "<font color=\"red\">Hai postato un commento meno di un minuto fa, attendi</font>"; 
        } 
        else if(response == "posted") 
        { 
         post_status.innerHTML = "<font color=\"green\">Post aggiunto con successo</font>"; 
         document.getElementById("newcommenttxt").value = ""; 
         document.getElementById("newcommenttxt").rows = 2; 
         document.getElementById("list-comments").innerHTML = '<br><br><br><center><img style="cursor: progress;" width="60px" height="60px" src="./styles/images/loading.gif"></center>'; 
         setTimeout("loadGagi()", 1000); 
         document.getElementById("reply_to_title").innerHTML = ""; 
         replyto = -1; 
        } 
       } 
      } 
      //xmlhttp.open("GET","./includes/postgagi?userid="+userid+"&replyto="+replyto+"&txt="+edited_comment,true); 
      xmlhttp.open("POST","./includes/postgagi",true); 
      xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8"); 
      xmlhttp.send("userid="+userid+"&replyto="+replyto+"&txt="+edited_comment+""); 
+0

可以節省您的字符使用phpMyAdmin數據庫(或類似)? –

+0

你能發佈你的php代碼嗎? – dpDesignz

+0

您是否已經嘗試在將數據保存到數據庫之前將數據編碼爲utf-8。您可以使用內置的php函數utf8_encode()。同時使100%確定你的mysql數據庫/表被設置爲utf8_unicode_ci,這樣才能正常工作。 – PhearOfRayne

回答

0

設置進行任何查詢之前連接編碼:

mysql_set_charset("utf8"); 
// or $mysqli->set_charset("utf8"); if you are using mysqli 
+0

這就是解決方案!謝謝你,兄弟! :D –

+0

@PietroAvolio如果問題得到解決,您應該接受答案 – Esailija