[PHP],[JS],[Mysql],[Ajax] problem kodowania w ajax

0

to jest skrypt typu edit in place oparty na yvoschaap’s editinplace.js

moim problemem są polskie znaki zamiast "ąść" otrzymuje w bazie "%u0105%u015B%u0107 " pliki i baza w utf8

<script type="text/javascript">
<!--
//script by http://www.yvoschaap.com

//XMLHttpRequest class function
function datosServidor() {
};
datosServidor.prototype.iniciar = function() {
	try {
		// Mozilla / Safari
		this._xh = new XMLHttpRequest();
	} catch (e) {
		// Explorer
		var _ieModelos = new Array(
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP'
		);
		var success = false;
		for (var i=0;i < _ieModelos.length && !success; i++) {
			try {
				this._xh = new ActiveXObject(_ieModelos[i]);
				success = true;
			} catch (e) {
			}
		}
		if ( !success ) {
			return false;
		}
		return true;
	}
}

datosServidor.prototype.ocupado = function() {
	estadoActual = this._xh.readyState;
	return (estadoActual && (estadoActual < 4));
}

datosServidor.prototype.procesa = function() {
	if (this._xh.readyState == 4 && this._xh.status == 200) {
		this.procesado = true;
	}
}

datosServidor.prototype.enviar = function(urlget,datos) {
	if (!this._xh) {
		this.iniciar();
	}
	if (!this.ocupado()) {
		this._xh.open("GET",urlget,false);
        		this._xh.send(datos);
		if (this._xh.readyState == 4 && this._xh.status == 200) {
			return this._xh.responseText;
		}
		
	}
	return false;
}


var urlBase = "editMysql.php";
var formVars = "";
var changing = false;


function fieldEnter(campo,evt,idfld) {
	evt = (evt) ? evt : window.event;
	if (evt.keyCode == 13 && campo.value!="") {
		elem = document.getElementById( idfld );
		remotos = new datosServidor;
		nt = remotos.enviar(urlBase + "?fieldname=" +encodeURI(elem.id)+ "&content="+encodeURI(campo.value)+"&"+formVars,"");
		//remove glow
		noLight(elem);
		elem.innerHTML = nt;
		changing = false;
		return false;
	} else {
		return true;
	}


}

function fieldBlur(campo,idfld) {
	if (campo.value!="") {
		elem = document.getElementById( idfld );
		remotos = new datosServidor;
		nt = remotos.enviar(urlBase + "?fieldname=" +escape(elem.id)+ "&content="+escape(campo.value)+"&"+formVars,"");
		elem.innerHTML = nt;
		changing = false;
		return false;
	}
}

//edit field created
function editBox(actual) {
	//alert(actual.nodeName+' '+changing);
	if(!changing){
		width = widthEl(actual.id) + 20;
		height =heightEl(actual.id) + 2;

		if(height < 40){
			if(width < 100)	width = 150;
			actual.innerHTML = "<input id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" maxlength=\"254\" type=\"text\" value=\"" + actual.innerHTML + "\" onkeypress=\"return fieldEnter(this,event,'" + actual.id + "')\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\" />";
		}else{
			if(width < 70) width = 90;
			if(height < 50) height = 50;
			actual.innerHTML = "<textarea name=\"textarea\" id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\">" + actual.innerHTML + "</textarea>";
		}
		changing = true;
	}

		actual.firstChild.focus();
}



//find all span tags with class editText and id as fieldname parsed to update script. add onclick function
function editbox_init(){
	if (!document.getElementsByTagName){ return; }
	var spans = document.getElementsByTagName("span");

	// loop through all span tags
	for (var i=0; i<spans.length; i++){
		var spn = spans[i];

        	if (((' '+spn.className+' ').indexOf("editText") != -1) && (spn.id)) {
			spn.onclick = function () { editBox(this); }
			spn.style.cursor = "pointer";
			spn.title = "Click to edit!";	
       		}

	}


}

//crossbrowser load function
function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Please upgrade your browser to use full functionality on this page");
  }
}



//get width of text element
function widthEl(span){

	if (document.layers){
	  w=document.layers[span].clip.width;
	} else if (document.all && !document.getElementById){
	  w=document.all[span].offsetWidth;
	} else if(document.getElementById){
	  w=document.getElementById(span).offsetWidth;
	}
return w;
}

//get height of text element
function heightEl(span){

	if (document.layers){
	  h=document.layers[span].clip.height;
	} else if (document.all && !document.getElementById){
	  h=document.all[span].offsetHeight;
	} else if(document.getElementById){
	  h=document.getElementById(span).offsetHeight;
	}
return h;
}

function highLight(span){
            span.parentNode.style.border = "2px solid #D1FDCD";
            span.parentNode.style.padding = "0";
            span.style.border = "1px solid #54CE43";          
}

function noLight(span){
        span.parentNode.style.border = "0px";
        span.parentNode.style.padding = "2px";
        span.style.border = "0px";   


}

//sets post/get vars for update
function setVarsForm(vars){
	formVars  = vars;
}
addEvent(window, "load", editbox_init);

-->
</script>
0

W pliku: editMysql.php masz zapewne <META> czy jakiś BOM. To nic nie da: AJAX nie rozumie tych oznaczeń z kilku prostych powodów. Na samej górze pliku editMysql.php zanim cokolwiek zrobisz dodaj linijkę:

header('Content-Type: text/html; charset=utf-8');

Powinno działać.

0

mam

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Content-Type: text/html; charset=utf-8");
0

editMysql.php, strona i wszystkie pliki JS muszą być w UTF-8. Zgaduję że gdzieś nie ma.

0

wrr nie ma opcji edycji
oba pliki sa w uft-8 bez BOM baza utf8
zawartosc pliku editMysql.php

<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Content-Type: text/html; charset=utf-8");

require "../pol.php";
connection ();

$fieldname = $_GET['fieldname']; 
$content = $_GET['content'];

if ( ( $fieldname[0] == 'a' ) && ( $fieldname[1] == '_' ) ) {
$myFilename = substr($fieldname, 2); 
$query = "UPDATE preg SET serwer = '$content' WHERE id = '$myFilename'";
} elseif  ( ( $fieldname[0] == 'b' ) && ( $fieldname[1] == '_' ) ) {
$myFilename = substr($fieldname, 2);
$query = "UPDATE preg SET konto = '$content' WHERE id = '$myFilename'";
} elseif  ( ( $fieldname[0] == 'c' ) && ( $fieldname[1] == '_' ) ) {
$myFilename = substr($fieldname, 2);
$query = "UPDATE preg SET nick = '$content' WHERE id = '$myFilename'";
} elseif  ( ( $fieldname[0] == 'd' ) && ( $fieldname[1] == '_' ) ) {
$myFilename = substr($fieldname, 2);
$query = "UPDATE preg SET powod = '$content' WHERE id = '$myFilename'";
}elseif  ( ( $fieldname[0] == 'e' ) && ( $fieldname[1] == '_' ) ) {
$myFilename = substr($fieldname, 2);
$query = "UPDATE preg SET ktory = '$content' WHERE id = '$myFilename'";
} elseif  ( ( $fieldname[0] == 'f' ) && ( $fieldname[1] == '_' ) ) {
$myFilename = substr($fieldname, 2);
$query = "UPDATE preg SET dowod = '$content' WHERE id = '$myFilename'";
} elseif  ( ( $fieldname[0] == 'g' ) && ( $fieldname[1] == '_' ) ) {
$myFilename = substr($fieldname, 2);
$query = "UPDATE preg SET kiedy = '$content' WHERE id = '$myFilename'";
}elseif  ( ( $fieldname[0] == 'h' ) && ( $fieldname[1] == '_' ) ) {
$myFilename = substr($fieldname, 2);
$query = "UPDATE preg SET ban = '$content' WHERE id = '$myFilename'";
} else {  
$myFilename = substr($fieldname, 2);
$query = "UPDATE preg SET kto = '$content' WHERE id = '$myFilename'";
}


mysql_query($query) or die(mysql_error());

mysql_close();

$fieldname = $_GET['fieldname'];
echo stripslashes(strip_tags($_GET['content'],"<br><p><img><a><br /><strong><em>"));
?> 

zaraz po laczeniu z baza danych jest " mysql_query("SET CHARSET utf8");
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_polish_ci'"); "
no i kluczowa zawartosc pliku edit.php

$wykonaj = mysql_query ( $zapytanie )or die('Błąd wykonania zapytania: '.mysql_error());
	while ( $row = mysql_fetch_array ( $wykonaj ) ) {
	   $id = $row[0];
echo" <tr>
 <td class=\"b\">$id</td>
 <td class=\"b\"><div id=\"change\"><span id=\"a_$id\" class=\"editText\">$row[1]</span></div></td>
 <td class=\"b\"><div id=\"change\"><span id=\"b_$id\" class=\"editText\">$row[2]</span></div></td>
 <td class=\"b\"><div id=\"change\"><span id=\"c_$id\" class=\"editText\">$row[3]</span></div></td>
 <td class=\"b\"><div id=\"change\"><span id=\"d_$id\" class=\"editText\">$row[4]</span></div></td>
 <td class=\"b\"><div id=\"change\"><span id=\"e_$id\" class=\"editText\">$row[5]</span></div></td>
 <td class=\"b\"><div id=\"change\"><span id=\"f_$id\" class=\"editText\">$row[6]</span></div></td>
 <td class=\"b\"><div id=\"change\"><span id=\"g_$id\" class=\"editText\">$row[7]</span></div></td>
 <td class=\"b\"><div id=\"change\"><span id=\"h_$id\" class=\"editText\">$row[8]</span></div></td>
 <td class=\"b\"><div id=\"change\"><span id=\"i_$id\" class=\"editText\">$row[9]</span></div></td>
";
echo ' </tr>';
	}
	echo "
</table>
<br>
<script type=\"text/javascript\">
setVarsForm(\"pageID=profileEdit&userID=$id&sessionID=$row[0]\");
</script>
";
0

Dla gości nie ma edycji. Anyway w pliku w którym [renderowany] jest JavaScript też musi być UTF-8.
Chodzi o plik który cytowałeś w #1 poście.

0

ma zarówno

mysql_query("SET CHARSET utf8");
    mysql_query("SET NAMES 'utf8' COLLATE 'utf8_polish_ci'"); "

jak i

header("Content-Type: text/html; charset=utf-8");

pierwszy post to wycinek pliku edit.php i jak pisałem ma on ustawione utf-8 bez bom
stąd mój wniosek że to JS za tym stoi

xmlHttp.overrideMimeType

przypuszczam ze to będę musiał użyć tylko gdzie i jak?

0

Ten plik:

<script type="text/javascript">
<!--
//script by http://www.yvoschaap.com

//XMLHttpRequest class function
function datosServidor() {
};
datosServidor.prototype.iniciar = function() {
        try {
                // Mozilla / Safari
                this._xh = new XMLHttpRequest();
        } catch (e) {
                // Explorer
                var _ieModelos = new Array(
                'MSXML2.XMLHTTP.5.0',
                'MSXML2.XMLHTTP.4.0',
                'MSXML2.XMLHTTP.3.0',
                'MSXML2.XMLHTTP',
                'Microsoft.XMLHTTP'
                );
                var success = false;
                for (var i=0;i < _ieModelos.length && !success; i++) {
                        try {
                                this._xh = new ActiveXObject(_ieModelos[i]);
                                success = true;
                        } catch (e) {
                        }
                }
                if ( !success ) {
                        return false;
                }
                return true;
        }
}

datosServidor.prototype.ocupado = function() {
        estadoActual = this._xh.readyState;
        return (estadoActual && (estadoActual < 4));
}

datosServidor.prototype.procesa = function() {
        if (this._xh.readyState == 4 && this._xh.status == 200) {
                this.procesado = true;
        }
}

datosServidor.prototype.enviar = function(urlget,datos) {
        if (!this._xh) {
                this.iniciar();
        }
        if (!this.ocupado()) {
                this._xh.open("GET",urlget,false);
                        this._xh.send(datos);
                if (this._xh.readyState == 4 && this._xh.status == 200) {
                        return this._xh.responseText;
                }
               
        }
        return false;
}


var urlBase = "editMysql.php";
var formVars = "";
var changing = false;


function fieldEnter(campo,evt,idfld) {
        evt = (evt) ? evt : window.event;
        if (evt.keyCode == 13 && campo.value!="") {
                elem = document.getElementById( idfld );
                remotos = new datosServidor;
                nt = remotos.enviar(urlBase + "?fieldname=" +encodeURI(elem.id)+ "&content="+encodeURI(campo.value)+"&"+formVars,"");
                //remove glow
                noLight(elem);
                elem.innerHTML = nt;
                changing = false;
                return false;
        } else {
                return true;
        }


}

function fieldBlur(campo,idfld) {
        if (campo.value!="") {
                elem = document.getElementById( idfld );
                remotos = new datosServidor;
                nt = remotos.enviar(urlBase + "?fieldname=" +escape(elem.id)+ "&content="+escape(campo.value)+"&"+formVars,"");
                elem.innerHTML = nt;
                changing = false;
                return false;
        }
}

//edit field created
function editBox(actual) {
        //alert(actual.nodeName+' '+changing);
        if(!changing){
                width = widthEl(actual.id) + 20;
                height =heightEl(actual.id) + 2;

                if(height < 40){
                        if(width < 100)        width = 150;
                        actual.innerHTML = "<input id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" maxlength=\"254\" type=\"text\" value=\"" + actual.innerHTML + "\" onkeypress=\"return fieldEnter(this,event,'" + actual.id + "')\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\" />";
                }else{
                        if(width < 70) width = 90;
                        if(height < 50) height = 50;
                        actual.innerHTML = "<textarea name=\"textarea\" id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\">" + actual.innerHTML + "</textarea>";
                }
                changing = true;
        }

                actual.firstChild.focus();
}



//find all span tags with class editText and id as fieldname parsed to update script. add onclick function
function editbox_init(){
        if (!document.getElementsByTagName){ return; }
        var spans = document.getElementsByTagName("span");

        // loop through all span tags
        for (var i=0; i<spans.length; i++){
                var spn = spans[i];

                if (((' '+spn.className+' ').indexOf("editText") != -1) && (spn.id)) {
                        spn.onclick = function () { editBox(this); }
                        spn.style.cursor = "pointer";
                        spn.title = "Click to edit!";       
                       }

        }


}

//crossbrowser load function
function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Please upgrade your browser to use full functionality on this page");
  }
}



//get width of text element
function widthEl(span){

        if (document.layers){
          w=document.layers[span].clip.width;
        } else if (document.all && !document.getElementById){
          w=document.all[span].offsetWidth;
        } else if(document.getElementById){
          w=document.getElementById(span).offsetWidth;
        }
return w;
}

//get height of text element
function heightEl(span){

        if (document.layers){
          h=document.layers[span].clip.height;
        } else if (document.all && !document.getElementById){
          h=document.all[span].offsetHeight;
        } else if(document.getElementById){
          h=document.getElementById(span).offsetHeight;
        }
return h;
}

function highLight(span){
            span.parentNode.style.border = "2px solid #D1FDCD";
            span.parentNode.style.padding = "0";
            span.style.border = "1px solid #54CE43";         
}

function noLight(span){
        span.parentNode.style.border = "0px";
        span.parentNode.style.padding = "2px";
        span.style.border = "0px";   


}

//sets post/get vars for update
function setVarsForm(vars){
        formVars  = vars;
}
addEvent(window, "load", editbox_init);

-->
</script>

Też musi być zakodowany w UTF-8 bez BOM.
Wszystkie pliki związane z projektem musisz zakodować w tym standardzie. Nie tylko te *.php

0

przecież już 3 razy pisałem że to fragment pliku edit.php który ma stosowne kodowanie (to sekcja z nagłówka jest tu wklejona tylko)

1 użytkowników online, w tym zalogowanych: 0, gości: 1