|
|
|
| Простейший пример AJAX. Некорректно происходит запись в БД кириллицы. Как это исправить?
index.html
<html>
<head>
<title></title>
<script type="text/javascript">
var httpRequest = createHttpRequest();
var resultId = '';
function createHttpRequest()
{
var httpRequest;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else{
httpRequest = new XMLHttpRequest();
}
return httpRequest;
}
function sendRequest(file, _resultId, getRequestProc)
{
var txt = document.getElementById("txt").value;
httpRequest.open('get', file+'?name=' + txt, true);
httpRequest.onreadystatechange = getRequestProc;
httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded charset=windows-1251");
httpRequest.send(null);
}
function getRequest()
{
if (httpRequest.readyState == 4){
window.document.getElementById('status').innerHTML = httpRequest.responseText;
}else{
document.getElementById('status').innerHTML = "<img src='pre.gif'>";
}
}
function olimg()
{
document.getElementById('status').innerHTML = "<img src='otp.gif' onclick=sendRequest('dvij.php','result',getRequest);>";
}
</script>
</head>
<body onload="olimg()">
<table>
<tr><td><input type="text" id="txt"></td>
<td id='status'></td>
<td id="result"></td>
</tr>
</table>
</body>
</html>
|
dvij.php
<?php
$host = "localhost";
$user = "root";
$pass = "qwerty";
if (isset($_GET['name'])) $data = $_GET['name'];
$con = mysql_connect($host, $user, $pass);
if ($con)
{
mysql_select_db("test", $con);
$q = mysql_query("INSERT INTO other (data) VALUES ('$data')"); //некорректно записывается в MySQL кириллицей
sleep(2);
header("Content-Type: text/html; charset=windows-1251");
if ($q) echo "Получилось";
else echo "Не получилось".mysql_error();
}
?>
|
| |
|
|
|
|
|
|
|
для: Bubba
(24.11.2008 в 22:10)
| | А если добавить mysql_query('SET NAMES "utf8"');
перед вставкой в таблицу?
таблица или поля в ней DEFAULT_CHARSET наверное cp1251 ,как и charset страницы ? | |
|
|
|
|
|
|
|
для: Bubba
(24.11.2008 в 22:10)
| | вопервых
header("Content-Type: text/html; charset=windows-1251"); в начало документа,
и так:
$data = iconv('utf-8', 'windows-1251', $_GET['name']);
|
.. | |
|
|
|
|
|
|
|
для: sl1p
(24.11.2008 в 22:52)
| | Ну тем не менее, ничего из предложенного не решило проблему. | |
|
|
|
|
|
|
|
для: Bubba
(25.11.2008 в 16:50)
| |
header('Content-type: application/xml; charset=utf-8');
header('Cache-Control: no-cache');
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
|
вставьте в php скрипт сверху, должно решить | |
|
|
|