Rozchodzi się o kodowanie znaków po wysłaniu danych POSTem przez XMLHTTPRequest.
Skrypt wygląda następująco:
1) Główna strona (fax.php) pobiera za pomocą XMLHTTPRequest wynik działania innej (editSMS.php) i umieszcza go w DIVie.
editSMS.php wyświetla formularz edycji.
2) Po zatwierdzeniu zmian w formularzu, strona fax.php, która wyświetla pobrany formularz z editSMS.php, przesyła za pomocą XMLHTTPRequest dane z formularza POSTem do editSMS.php.
3) editSMS.php pobiera dane przesłane POSTem, zapisuje je i ponownie wyświetla formularz, który to znowu wyświetla fax.php.
Do pobierania formularza, który wyświetla strone editSMS.php używam funkcji getAXAH() - elementContainer w tej funkcji to nazwa DIVa, w który zostanie wklejona treść pobranej strony.
Strona editSMS.php wypluwa TYLKO formularz HTMLowy.
Za pomocą postAXAH() wysyłam POSTem dane z formularza XMLHTTPRequestem do skryptu editSMS.php.
params w tej funkcji określa zbiór parametrów POSTa (czyli dane z formularza) w postaci var=val&var2=val2 etc..
Kod
<script type="text/javascript">
function getNewHttpObject() {
var objType = false;
try {
objType = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
try {
objType = new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {
objType = new XMLHttpRequest();
}
}
return objType;
}
function getAXAH(url,elementContainer){
document.getElementById(elementContainer).innerHTML = '<blink class="redtxt">Ładowanie danych...<\/blink>';
var theHttpRequest = getNewHttpObject();
theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
theHttpRequest.open("GET", url);
theHttpRequest.setRequestHeader('Content-Type', 'text/html; charset=iso-8859-2');
theHttpRequest.send(false);
function processAXAH(elementContainer){
if (theHttpRequest.readyState == 4) {
if (theHttpRequest.status == 200) {
document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
} else {
document.getElementById(elementContainer).innerHTML="<p><span class='redtxt'>Error!<\/span> Opis błędu: " + theHttpRequest.statusText +"<\/p>";
}
}
}
}
function postAXAH(url, elementContainer, params) {
document.getElementById(elementContainer).innerHTML = '<blink class="redtxt">Ładowanie danych...<\/blink>';
var theHttpRequest = getNewHttpObject();
theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
theHttpRequest.open("POST", url);
theHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-2');
theHttpRequest.send(params);
function processAXAH(elementContainer){
if (theHttpRequest.readyState == 4) {
if (theHttpRequest.status == 200) {
document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
} else {
document.getElementById(elementContainer).innerHTML="<p><span class='redtxt'>Error!<\/span> Opis błędu: " + theHttpRequest.statusText +"<\/p>";
}
}
}
}
</script>
function getNewHttpObject() {
var objType = false;
try {
objType = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
try {
objType = new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {
objType = new XMLHttpRequest();
}
}
return objType;
}
function getAXAH(url,elementContainer){
document.getElementById(elementContainer).innerHTML = '<blink class="redtxt">Ładowanie danych...<\/blink>';
var theHttpRequest = getNewHttpObject();
theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
theHttpRequest.open("GET", url);
theHttpRequest.setRequestHeader('Content-Type', 'text/html; charset=iso-8859-2');
theHttpRequest.send(false);
function processAXAH(elementContainer){
if (theHttpRequest.readyState == 4) {
if (theHttpRequest.status == 200) {
document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
} else {
document.getElementById(elementContainer).innerHTML="<p><span class='redtxt'>Error!<\/span> Opis błędu: " + theHttpRequest.statusText +"<\/p>";
}
}
}
}
function postAXAH(url, elementContainer, params) {
document.getElementById(elementContainer).innerHTML = '<blink class="redtxt">Ładowanie danych...<\/blink>';
var theHttpRequest = getNewHttpObject();
theHttpRequest.onreadystatechange = function() {processAXAH(elementContainer);};
theHttpRequest.open("POST", url);
theHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=iso-8859-2');
theHttpRequest.send(params);
function processAXAH(elementContainer){
if (theHttpRequest.readyState == 4) {
if (theHttpRequest.status == 200) {
document.getElementById(elementContainer).innerHTML = theHttpRequest.responseText;
} else {
document.getElementById(elementContainer).innerHTML="<p><span class='redtxt'>Error!<\/span> Opis błędu: " + theHttpRequest.statusText +"<\/p>";
}
}
}
}
</script>
Powyższy kod leży w pliku fax.php, skąd odwołuje się do niego min. formularz z editSMS.php podczas kliknięcia na przycisk wysłania danych.
A teraz do rzeczy:
Podczas poberania formularza po przez getAXAH() kodowanie polskich znaków działa (dodatkowo w editSMS.php dodałem header z Content-typem ustawiający charset na iso-8859-2) - działa tzn. wyświetla polskie znaki w formularzu.
Problem jest dopiero gdy wyedytuje formularz i w jakieś pole wstawie polskie znaki. Wtedy po zapisaniu formularza (wysłania ich POSTem przez postAXAH()) - po wysłaniu, skrypt editSMS.php wyświetla ponownie formularz z nowymi danymi oraz infromacją o dodaniu danych - pojawiają się już krzaki.
Dokładnie to wygląda tak, że dane wysłane przez postAXAH() są w formacie utf-8 i takie też, trafiają do baz - stąd ponowne wyświetlenie formularza powoduje pokazanie krzaków.
Dodałem do setRequestHeader() charset iso-8859-2 i nadal nic.
Nie wiem jak to obsłużyć aby XMLHTTPReqest wysłało te dane zakodwane w iso a ne w utf'ie
Ma ktoś jakiś pomysł ?
