Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Estou desenvolvendo uma combo utilizando as dicas que li no Mozzila Developers. Consegui fazer funcionar muito bem no Firefox, mas no IE não funciona de jeito nenhum. Não sei qual o problema. Abaixo está o código para que vocês possam analisar para mim. Desde já, obrigado!
A propósito, o sistema retorna valores, só não insere no select.
Quando ponho alert(http_request.responseText); funciona que é uma beleza, mas não popula o select. Isso ocorre somente no IE, pois no FF tá funcionando super bem!
combo.php
<?php header("Content-Type: text/html; charset=ISO-8859-1",true);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title><script type="text/javascript" language="javascript"> var http_request = false; function fazerPedido(pais) { http_request = false; document.getElementById("select_estado").style.display = 'none'; document.getElementById("select_pais").style.display = 'none'; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); } } if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { alert('Erro ao criar instancia XMLHTTP'); return false; } var select_pais=document.getElementById("select_pais") select_pais.innerHTML='<div class="carregando">carregando...</div>' //caso queira pegar variaveis por GET //http_request.open('GET', url, true); //Caso queira enviar variaveis via POST http_request.open("POST", "pedido.php", true); http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1"); var exibir = "pais="+pais.options[pais.selectedIndex].value; http_request.send(exibir); http_request.onreadystatechange = alertContents; } function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { //alert(http_request.responseText); //Exibe o texto carregando no div conteúdo //alert(http_request.responseText); //Lê o texto var texto=http_request.responseText texto=texto.replace(/\+/g," "); texto=unescape(texto); //Exibe o texto no div conteúdo document.getElementById("select_pais").style.display = 'block'; var select_pais=document.getElementById("select_pais"); select_pais.innerHTML=texto //Limpa o campo cidades var select_estado=document.getElementById("select_estado").style.display = 'none'; select_estado.innerHTML=null } else { alert('There was a problem with the request.'); } } } //Novo pedido var http_request = false; function fazerPedido2(estado) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); } } else if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { alert('Erro ao criar instancia XMLHTTP'); return false; } document.getElementById("select_estado").style.display = 'block'; var select_estado=document.getElementById("select_estado") select_estado.innerHTML='<div class="carregando">carregando...</div>' //caso queira pegar variaveis por GET //http_request.open('GET', url, true); //Caso queira enviar variaveis via POST http_request.open("POST", "pedido.php", true); http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1"); var exibir = "estado="+estado.options[estado.selectedIndex].value; http_request.send(exibir); http_request.onreadystatechange = alertContents2; } function alertContents2() { if (http_request.readyState == 4) { if (http_request.status == 200) { //alert(http_request.responseText); //Exibe o texto carregando no div conteúdo //alert(http_request.responseText); //Lê o texto var texto=http_request.responseText texto=texto.replace(/\+/g," "); texto=unescape(texto); //Exibe o texto no div conteúdo var select_estado=document.getElementById("select_estado") select_estado.innerHTML=texto } else { alert('There was a problem with the request.'); } } } function limpar() { document.getElementById("select_pais").style.display = 'none'; document.getElementById("select_estado").style.display = 'none'; } </script></head><body><select name="pais" onchange="fazerPedido(this);"> <option>selecione</option> <option value="BR">Brasil</option> <option value="AR">Argentina</option></select><select name="estado" id="select_pais" style="display:none;" onchange="fazerPedido2(this);"> <option disabled="disabled" selected="selected" value="">Selecione o país</option></select><select name="cidade" id="select_estado" style="display:none;"> <option disabled="disabled" selected="selected" value="">Selecione o estado</option></select></body></html>
pedido.php
<?php header("Content-Type: text/html; charset=ISO-8859-1",true); mysql_connect("localhost", "vebersol", "senha"); mysql_select_db("ajax"); $pais = $_POST['pais']; if($pais) { $sql = mysql_query("SELECT * FROM estados WHERE pais = '$pais' ORDER BY estado") or die(mysql_error()); $total = mysql_num_rows($sql); if($total > 0) { echo "<option disabled=\"disabled\" selected=\"selected\" value=\"\">Selecione o estado</option>"; while($l = mysql_fetch_assoc($sql)) { $abr = $l['uf']; $nme = $l['estado']; echo "<option value=\"$abr\">$nme</option>"; } } else { echo "<option value=\"\" disabled=\"disabled\" >Não há valores</option>"; } } $estado = $_POST['estado']; if($estado) { $sql = mysql_query("SELECT * FROM cidades WHERE uf = '$estado' ORDER BY cidade") or die(mysql_error()); $total = mysql_num_rows($sql); if($total > 0) { echo "<option disabled=\"disabled\" selected=\"selected\" value=\"\">Selecione a cidade</option>"; while($l = mysql_fetch_assoc($sql)) { $nmc = $l['cidade']; echo "<option value=\"$nmc\">$nmc</option>"; } } else { echo "<option value=\"\" disabled=\"disabled\" >Não há valores</option>"; } } ?>Carregando comentários...