我想将我输入的malayalam文本保存到textarea到mysql。但是当我尝试接受表单提交中的malayalam内容时,它会像à'¾àμ€àà,à'àà'-à'-à'œà'œ字符一样返回。但是我使用了utf-8编码。谁能帮我。我所做的代码如下:
<?php
$dbLink = mysql_connect('localhost', 'root', 'root');
mysql_query("SET character_set_results=utf8", $dbLink);
mysql_query("SET character_set_connection=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF8');
mysql_select_db('test',$dblink);
mysql_query("set names 'utf8'",$dbLink);
if(isset($_POST['txt1']))
{
$txt= $_POST['txt1']; // get the unicode text from a submit action.
$cQry= "insert into news (news) values ('$txt')" ;
$cresult = mysql_query($cQry,$dbLink);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8" />
</head>
<body>
<input type="hidden" id="MicrosoftILITWebEmbedInfo" attachMode="optout" value="">
<script type="text/javascript" src="http://ilit.microsoft.com/bookmarklet/script/Malayalam.js" defer="defer"></script>
<form action="" method="post" accept-charset="utf-8">
<textarea rows="10" charset="utf-8" cols="20" name="txt1"></textarea>
<input type="submit" value="Submit">
</form>
</body>
</html>
提前谢谢你。
答案 0 :(得分:2)
change malayalam text to unicode in backend them save to database .
use below function, onblur event to convert text to unicode:
function convertToHex(num) {
var code ;
var code2 ='' ;
var code3 ='';
for (i=0;i<num.length;i++)
{
code = num.charCodeAt(i).toString(16).toUpperCase();
code3 = '';
for(j=0; j<4-code.length; j++)
{
code3 += '0';
}
code2 +='%u'+ code3 +code;
}
return code2;
}
答案 1 :(得分:0)
我为获得结果所做的代码更改是:
<?php
// Using mysql
$dbLink=mysql_connect('localhost', 'root', 'root') or die('Could not connect: ' . mysql_error());
mysql_select_db('test') or die('Could not select database');
mysql_query("set names 'utf8'",$dbLink);
if (!empty($_POST['ta'])) {
$val= $_POST['ta'];
$val=utf8_encode($val);
$qry="insert into news(id,news)values ('','$val')";
mysql_query($qry);
}
$result = mysql_query("SELECT * FROM news");
while ($row = mysql_fetch_row($result)) {
echo $row[1]; echo "<br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test</title>
</head>
<body>
<form enctype="multipart/form-data" method="post" action="index.php">
<textarea name="ta" rows="6" cols="6"></textarea>
<input type="submit" />
</form>
</body>
</html>
答案 2 :(得分:0)
只需将名称归类设置为数据库中的utf8-general_ci即可。您的PHP代码不需要任何更改。