0

i'm passing some values to a php file with ajax using the POST method.

When the php file receives special chars like à è ì it saw it correctly, but when i put it into a query they saves bad on the database.

Where i'm wrong? I tried to change charset to the mysql table... but nothing.

Here's my code.

Ajax File:

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+"");
4

1 回答 1

0

在进行任何查询之前设置连接编码:

mysql_set_charset("utf8");
// or $mysqli->set_charset( "utf8" ); if you are using mysqli
于 2012-12-16T12:18:57.190 回答