Ciao a tutti! Vi scrivo perchè ho un problema che non riesco a risolvere.... Dunque, ho una pagina con una casella di riepilogo, la classica tendina, nella quale devono essere caricati dei valori mediante una richesta asincrona. Questi valori derivano da alcune query fatte sulla pagina lato server.
Per semplificare il problema, nella pagina php ho inserito semplicemente una echo con i valori che mi dovrebbero ritornare sul lato client.
Il problema è che quando i valori vengono ricevuti e inseriti nella <select>, nella tendina compaiono dei caratteri strani.... Faccio un esempio:
Marche disponibili:
-----------------------
<- carattere vuto
Asus
AsRock
Intel
Msi
Gigabyte
-----------------------
Pagina lato client: index.php
<?php
include('inc/conn.inc.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="it">
<head>
<title></title>
<!-- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" >
var http = createRequestObject();
var where = (navigator.appName == "Microsoft Internet Explorer") ? -1 : null;
function createRequestObject() {
var ro;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
ro = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
ro = new XMLHttpRequest();
}
return ro;
}
function addElement(ogg,val,text) {
var newOption;
newOption = document.createElement("option");
newOption.value = val;
newOption.text = text;
ogg.add(newOption, where);
}
function removeElement(ogg) {
if (ogg.options.length > 0) {
while (ogg.options.length) {
ogg.remove(0);
}
}
}
function disabledElement(ogg,val){
document.getElementById(ogg).disabled = val;
}
function richiediMarca(oggId){
var id = document.getElementById(oggId).value;
http.open('GET','prova.php', true);
http.onreadystatechange = rispostaMarca;
http.send(null);
}
function rispostaMarca() {
var Marca = document.getElementById('m1');
if (http.readyState == 4) {
if (http.status == 200) {
var response = http.responseText;
removeElement(Marca);
var elemento = response.split(',');
max = elemento.length;
addElement(Marca,'sel','Seleziona una marca:');
for(x=0;x<max;x++) {
var val = elemento[x].split(',')
addElement(Marca,val[0],val[0]);
}
disabledElement('m1',false);
}
else {
addElement(Marca,'sel','Caricamento...');
}
}
}
</script>
</head>
<body onload="richiediMarca('m1');">
<form name="form">
Marche disponibili:<br>
<select name="m1" id="m1">
<option value="sel" selected="selected"></option>
</select>
</form>
</body>
</html>
Pagina lato server: prova.php
<?php
$prova=",Asus,AsRock,Intel,Msi,GigaByte,";
//echo utf8_encode($prova);
echo ($prova);
?>
Ho provato varie funzioni di codifica, tipo utf8_encode, ma niente.
Un piccola osservazione: Se sostituisco:
var response = http.responseText;
con
var response = ',Asus,AsRock,Intel,Msi,GigaByte,';
i valori vengono inseriti correttamente nella tendina, senza problemi di caratteri vuoti o strani.
Aspetto vostri suggerimenti!
Grazie