我有页面(编码'windows-1251').php与$ .ajax发件人:
<div style='float:left;' id='commentRegistration'> <input id='addNewCommentUserName' name="name" type="text" size="20" maxlength="30" style="border:1px solid #AAA; width:208px;" value="<? echo "$_SESSION[login]"; ?>" /> <textarea id='addNewCommentText' name="text" style="width: 406px; height: 50px; resize: vertical; border:1px solid #AAA;"></textarea> <input id='addNewCommentId' name="id" type="hidden" value="<? echo"$myrow[id]"; ?>"> <input id='addNewCommentCodeGen' type='text' name='code' maxlength='6' style='border:1px solid #AAA; width:47px;'> <input id='addNewCommentBtn' class='button' style='width:208px; padding-top:5px; padding-bottom:5px;' type='submit' name='submit' value='Добавить комментарий'> <script> $(document).ready(function(){ var name = document.getElementById('addNewCommentUserName'); var text = document.getElementById('addNewCommentText'); var id = document.getElementById('addNewCommentId'); var code = document.getElementById('addNewCommentCodeGen'); $('#addNewCommentBtn').bind('click',function(){ $.ajax({ type: "POST", url: "comment.php", data: { 'name':name.value, 'text':text.value, 'id':id.value, 'code':code.value } }).done(function( msg ) { }); }); }); </script> comment.php: <? include ("blocks/bd.php"); session_start(); header("Content-type: text/html; charset=UTF-8"); if (isset($_POST['name'])) { $name = $_POST['name']; if ($name == '') { unset($name);} } if (isset($_POST['text'])) { $text = $_POST['text']; if ($text == '') { unset($text);} } if (isset($_POST['code'])) { $code = $_POST['code']; if ($code == '') { unset($code);} } if (isset($_POST['id'])) { $id = $_POST['id']; if ($id == '') { unset($id);} } if (empty($name) or empty($text) or empty($code) or empty($id)) { exit ("ErrorEmptyInfo"); } $name = stripslashes($name); $name = htmlspecialchars($name); $text = stripslashes($text); $text = htmlspecialchars($text); $name = trim($name); $text = trim($text); $mail = "test@mail.ru"; $subject = "text text text"; $message = "text text text"; if (mail($mail, $subject, $message, "Content-type:text/plain; Charset=windows-1251\r\n" )) { $dateNow = date("Y-m-d"); iconv("UTF-8", "windows-1251", $text); iconv("UTF-8", "windows-1251", $name); $addComment = mysql_query("INSERT INTO comments (post,author,text,date) VALUES ('$id','$name','$text','$dateNow')",$db); echo "SUCCESS"; } ?>在数据库$ text和$ name写得不正确(编码问题)。 我知道$ .ajax发送UTF-8编码。 要将编码更改为windows-1251,我使用iconv(“UTF-8”,“windows-1251”,$ text); 不工作 我如何强制使用windows-1251正确写入my_sql数据库字符串????
I have the page (encoding 'windows-1251') .php with $.ajax sender:
<div style='float:left;' id='commentRegistration'> <input id='addNewCommentUserName' name="name" type="text" size="20" maxlength="30" style="border:1px solid #AAA; width:208px;" value="<? echo "$_SESSION[login]"; ?>" /> <textarea id='addNewCommentText' name="text" style="width: 406px; height: 50px; resize: vertical; border:1px solid #AAA;"></textarea> <input id='addNewCommentId' name="id" type="hidden" value="<? echo"$myrow[id]"; ?>"> <input id='addNewCommentCodeGen' type='text' name='code' maxlength='6' style='border:1px solid #AAA; width:47px;'> <input id='addNewCommentBtn' class='button' style='width:208px; padding-top:5px; padding-bottom:5px;' type='submit' name='submit' value='Добавить комментарий'> <script> $(document).ready(function(){ var name = document.getElementById('addNewCommentUserName'); var text = document.getElementById('addNewCommentText'); var id = document.getElementById('addNewCommentId'); var code = document.getElementById('addNewCommentCodeGen'); $('#addNewCommentBtn').bind('click',function(){ $.ajax({ type: "POST", url: "comment.php", data: { 'name':name.value, 'text':text.value, 'id':id.value, 'code':code.value } }).done(function( msg ) { }); }); }); </script>comment.php: <? include ("blocks/bd.php"); session_start(); header("Content-type: text/html; charset=UTF-8"); if (isset($_POST['name'])) { $name = $_POST['name']; if ($name == '') { unset($name);} } if (isset($_POST['text'])) { $text = $_POST['text']; if ($text == '') { unset($text);} } if (isset($_POST['code'])) { $code = $_POST['code']; if ($code == '') { unset($code);} } if (isset($_POST['id'])) { $id = $_POST['id']; if ($id == '') { unset($id);} } if (empty($name) or empty($text) or empty($code) or empty($id)) { exit ("ErrorEmptyInfo"); } $name = stripslashes($name); $name = htmlspecialchars($name); $text = stripslashes($text); $text = htmlspecialchars($text); $name = trim($name); $text = trim($text); $mail = "test@mail.ru"; $subject = "text text text"; $message = "text text text"; if (mail($mail, $subject, $message, "Content-type:text/plain; Charset=windows-1251\r\n" )) { $dateNow = date("Y-m-d"); iconv("UTF-8", "windows-1251", $text); iconv("UTF-8", "windows-1251", $name); $addComment = mysql_query("INSERT INTO comments (post,author,text,date) VALUES ('$id','$name','$text','$dateNow')",$db); echo "SUCCESS"; } ?>
In database $text and $name writes not correctly (encoding problems). I understand that $.ajax send UTF-8 encoding. To change encoding to windows-1251 I use iconv("UTF-8", "windows-1251", $text); not working. How I force to write into my_sql database string with windows-1251 correctly????
最满意答案
这可能有所帮助:
$.ajax({ type: "POST", contentType: "application/json; charset=windows-1251", ....另外要强制MySQL使用windows-1251编码,添加此行一旦连接到数据库后:
mysql_query('SET NAMES "cp1251"');更新:
你可以使用follow而不是iconv ,这可能有用:
$text = mb_convert_encoding($text, 'Windows-1251');要么:
$text = mb_convert_encoding($text, 'Windows-1251', 'UTF-8');第二次更新
您可能需要注释掉这些行。 因为你的php内部编码不是windows-1251 ,当你在字符串上运行一些可能会产生一些问题的东西:
/* Temporary commented out, to see if it has any effects $name = stripslashes($name); $name = htmlspecialchars($name); $text = stripslashes($text); $text = htmlspecialchars($text); $name = trim($name); $text = trim($text); */This might help:
$.ajax({ type: "POST", contentType: "application/json; charset=windows-1251", ....Also to force MySQL to use windows-1251 encoding, add this line Once after you connect to your database:
mysql_query('SET NAMES "cp1251"');Update:
You can use the follow instead of iconv, that might work:
$text = mb_convert_encoding($text, 'Windows-1251');Or:
$text = mb_convert_encoding($text, 'Windows-1251', 'UTF-8');2nd Update
You might need to comment out these lines. Because your php internal encoding is not windows-1251, when you operating some stuff on your strings that might make some problems:
/* Temporary commented out, to see if it has any effects $name = stripslashes($name); $name = htmlspecialchars($name); $text = stripslashes($text); $text = htmlspecialchars($text); $name = trim($name); $text = trim($text); */更多推荐
发布评论