Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Vadio

busca CEP funciona no firefox mas não funciona no IE6

Recommended Posts

Criei uma função p/ pegar cep pode ajudar a muitos ae, mas preciso contornar esse erro no IE6, podem me ajudar?

-> acredito q o erro esteja aí no cep.html

-> tem q deixar como dhtml/xhtml esse cep.html?

obs: já tentei por onblur/onBlur/ONBLUR etc etc etc..

 

 

cep.html

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="cep.js"></script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>
  <input name="cep" type="text" id="cep_campo" onBlur="java script: buscar_cep(this.value);" size="10" maxlength="8" />
  </label>
</form>
<div id="cep"></div>
<p> </p>
</body>
</html>

cep.js

/**
 * @author Eric Rodrigo de Freitas
 */
//-> INICIA AJAX

function iniciaAjax(){
	if(window.ActiveXObject) var ajax = new ActiveXObject('Microsoft.XMLHTTP');
	else var ajax = new XMLHttpRequest();
	return ajax;
}

// Abrir Tela Carregando

function carregando_cep(div,msg){
	document.getElementById(div).innerHTML = msg;
}

// Busca do CEP (Requisição AJAX) -

function buscar_cep(cep){
	ajax = iniciaAjax();
	ajax.onreadystatechange = popula_cep;
	ajax.open('GET','xml_cep.php?cep='+cep);
	ajax.send(null);
}

// Popula os Campos do Formulário

function popula_cep(){
	carregando_cep('cep','Carregando');
	if (ajax.readyState == 4) {
		if (ajax.status == 200) {

			var xml = ajax.responseXML; 		  // - recebe XML
			
			if (xml != null) { 					//   - verifica se XML não está vazio
				var nofilho = xml.hasChildNodes;
				if (nofilho) {				  //	 - verifica se tem nos filhos
					
					var nos = xml.getElementsByTagName('cep');
					
					for (i = 0; i < nos.length; i++) {
						
						if (window.ActiveXObject) {

							var cor_msg = nos[i].childNodes[0].firstChild.nodeValue;
							var msg = nos[i].childNodes[1].firstChild.nodeValue;
							var cidade = nos[i].childNodes[2].firstChild.nodeValue;
							var uf = nos[i].childNodes[3].firstChild.nodeValue;
							var tipo = nos[i].childNodes[4].firstChild.nodeValue;
							var logradouro = nos[i].childNodes[5].firstChild.nodeValue;
							var bairro = nos[i].childNodes[6].firstChild.nodeValue;
						}
						else {
							var cor_msg 	= nos[i].childNodes[1].firstChild.nodeValue;
							var msg 		= nos[i].childNodes[3].firstChild.nodeValue;
							var cidade 		= nos[i].childNodes[5].firstChild.nodeValue;
							var uf 			= nos[i].childNodes[7].firstChild.nodeValue;
							var tipo 		= nos[i].childNodes[9].firstChild.nodeValue;
							var logradouro 	= nos[i].childNodes[11].firstChild.nodeValue;
							var bairro 		= nos[i].childNodes[13].firstChild.nodeValue;
						}
					}
					var mensagem = '<font color="'+cor_msg+'">'+msg+'</font><br>';
						mensagem+= tipo+': '+logradouro+'<br>';
						mensagem+= 'Bairro: '+bairro+'<br>';
						mensagem+= 'Cidade: '+cidade+' - UF:'+uf+'<br>';
						
				}
				
			}	
		}else var mensagem = 'erro na conexão com o servidor \\n'+ajax.statusText;
	}
	carregando_cep('cep',mensagem);
}
xml_cep.php

<?php   
 /* 
  *  Função de busca de Endereço pelo CEP 
  *  -   Desenvolvido Felipe Olivaes para ajaxbox.com.br 
  *  -   Utilizando WebService de CEP da republicavirtual.com.br 
  *  -   Alterado por Eric Rodrigo de Freitas em 22/11/2007 
  */  
 
class busca_cep{ 
 function busca_cep($cep){  
	$resultado = @file_get_contents('http://republicavirtual.com.br/web_cep.php?cep='.urlencode($cep).'&formato=query_string');  
	if(!$resultado){  
		$resultado = "&resultado=0&resultado_txt=erro+ao+buscar+cep";  
	}  
	parse_str($resultado, $_retorno);
	
	/*
	 *  - Cria XMl
	 */ 
 	header("Content-Type: application/xml");
	$_xml  = '<?xml version="1.0" encoding="ISO-8859-1" ?>'."\r\n";  
 	

	$_xml.= "\t"."<cep>\r\n";
	switch($_retorno['resultado']){  
 		case '2':
 			
 			$_xml_.="\t\t".'<cor_msg>green</cor_msg>'."\r\n"; //-> Define Cor da mensagem
 			$_xml.= "\t\t".'<msg>Cidade com logradouro único</msg>'."\r\n";
 			$_xml.= "\t\t".'<cidade>'.$_retorno['cidade'].'</cidade>'."\r\n";
	 		$_xml.= "\t\t".'<uf>'.$_retorno['uf'].'</uf>'."\r\n";
	 		
 			break;  
	 	  
	 	case '1':
	 		
	 		$_xml.= "\t\t".'<cor_msg>green</cor_msg>'."\r\n"; //-> Define Cor da mensagem
	 		$_xml.= "\t\t".'<msg>Cidade com logradouro completo</msg>'."\r\n";
	 		$_xml.= "\t\t".'<cidade>'.$_retorno['cidade'].'</cidade>'."\r\n";
	 		$_xml.= "\t\t".'<uf>'.$_retorno['uf'].'</uf>'."\r\n"; 
	 		$_xml.= "\t\t".'<tipo_logradouro>'.$_retorno['tipo_logradouro'].'</tipo_logradouro>'."\r\n";
	 		$_xml.= "\t\t".'<logradouro>'.$_retorno['logradouro'].'</logradouro>'."\r\n";
	 		$_xml.= "\t\t".'<bairro>'.$_retorno['bairro'].'</bairro>'."\r\n";

	 		break;  
	   
	 	default:
	 		
	 		$_xml_.="\t\t".'<cor_msg>red</cor_msg>'."\r\n";  //-> Define Cor da mensagem
		 	$_xml.= "\t\t".'<msg>Falha ao buscar CEP!</msg>'."\r\n"; 
		 	
	 	break;  
 	}
 	$_xml.= "\t"."</cep>\r\n";

 	
 	echo $_xml;
 }
}

$_cep = new busca_cep($_GET['cep']);

?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Criei uma função p/ pegar cep pode ajudar a muitos ae, mas preciso contornar esse erro no IE6, podem me ajudar?

-> acredito q o erro esteja aí no cep.html

-> tem q deixar como dhtml/xhtml esse cep.html?

obs: já tentei por onblur/onBlur/ONBLUR etc etc etc..

 

 

cep.html

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="cep.js"></script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <label>
  <input name="cep" type="text" id="cep_campo" onBlur="java script: buscar_cep(this.value);" size="10" maxlength="8" />
  </label>
</form>
<div id="cep"></div>
<p> </p>
</body>
</html>

cep.js

/**
 * @author Eric Rodrigo de Freitas
 */
//-> INICIA AJAX

function iniciaAjax(){
	if(window.ActiveXObject) var ajax = new ActiveXObject('Microsoft.XMLHTTP');
	else var ajax = new XMLHttpRequest();
	return ajax;
}

// Abrir Tela Carregando

function carregando_cep(div,msg){
	document.getElementById(div).innerHTML = msg;
}

// Busca do CEP (Requisição AJAX) -

function buscar_cep(cep){
	ajax = iniciaAjax();
	ajax.onreadystatechange = popula_cep;
	ajax.open('GET','xml_cep.php?cep='+cep);
	ajax.send(null);
}

// Popula os Campos do Formulário

function popula_cep(){
	carregando_cep('cep','Carregando');
	if (ajax.readyState == 4) {
		if (ajax.status == 200) {

			var xml = ajax.responseXML; 		  // - recebe XML
			
			if (xml != null) { 					//   - verifica se XML não está vazio
				var nofilho = xml.hasChildNodes;
				if (nofilho) {				  //	 - verifica se tem nos filhos
					
					var nos = xml.getElementsByTagName('cep');
					
					for (i = 0; i < nos.length; i++) {
						
						if (window.ActiveXObject) {

							var cor_msg = nos[i].childNodes[0].firstChild.nodeValue;
							var msg = nos[i].childNodes[1].firstChild.nodeValue;
							var cidade = nos[i].childNodes[2].firstChild.nodeValue;
							var uf = nos[i].childNodes[3].firstChild.nodeValue;
							var tipo = nos[i].childNodes[4].firstChild.nodeValue;
							var logradouro = nos[i].childNodes[5].firstChild.nodeValue;
							var bairro = nos[i].childNodes[6].firstChild.nodeValue;
						}
						else {
							var cor_msg 	= nos[i].childNodes[1].firstChild.nodeValue;
							var msg 		= nos[i].childNodes[3].firstChild.nodeValue;
							var cidade 		= nos[i].childNodes[5].firstChild.nodeValue;
							var uf 			= nos[i].childNodes[7].firstChild.nodeValue;
							var tipo 		= nos[i].childNodes[9].firstChild.nodeValue;
							var logradouro 	= nos[i].childNodes[11].firstChild.nodeValue;
							var bairro 		= nos[i].childNodes[13].firstChild.nodeValue;
						}
					}
					var mensagem = '<font color="'+cor_msg+'">'+msg+'</font><br>';
						mensagem+= tipo+': '+logradouro+'<br>';
						mensagem+= 'Bairro: '+bairro+'<br>';
						mensagem+= 'Cidade: '+cidade+' - UF:'+uf+'<br>';
						
				}
				
			}	
		}else var mensagem = 'erro na conexão com o servidor \\n'+ajax.statusText;
	}
	carregando_cep('cep',mensagem);
}
xml_cep.php

<?php   
 /* 
  *  Função de busca de Endereço pelo CEP 
  *  -   Desenvolvido Felipe Olivaes para ajaxbox.com.br 
  *  -   Utilizando WebService de CEP da republicavirtual.com.br 
  *  -   Alterado por Eric Rodrigo de Freitas em 22/11/2007 
  */  
 
class busca_cep{ 
 function busca_cep($cep){  
	$resultado = @file_get_contents('http://republicavirtual.com.br/web_cep.php?cep='.urlencode($cep).'&formato=query_string');  
	if(!$resultado){  
		$resultado = "&resultado=0&resultado_txt=erro+ao+buscar+cep";  
	}  
	parse_str($resultado, $_retorno);
	
	/*
	 *  - Cria XMl
	 */ 
 	header("Content-Type: application/xml");
	$_xml  = '<?xml version="1.0" encoding="ISO-8859-1" ?>'."\r\n";  
 	

	$_xml.= "\t"."<cep>\r\n";
	switch($_retorno['resultado']){  
 		case '2':
 			
 			$_xml_.="\t\t".'<cor_msg>green</cor_msg>'."\r\n"; //-> Define Cor da mensagem
 			$_xml.= "\t\t".'<msg>Cidade com logradouro único</msg>'."\r\n";
 			$_xml.= "\t\t".'<cidade>'.$_retorno['cidade'].'</cidade>'."\r\n";
	 		$_xml.= "\t\t".'<uf>'.$_retorno['uf'].'</uf>'."\r\n";
	 		
 			break;  
	 	  
	 	case '1':
	 		
	 		$_xml.= "\t\t".'<cor_msg>green</cor_msg>'."\r\n"; //-> Define Cor da mensagem
	 		$_xml.= "\t\t".'<msg>Cidade com logradouro completo</msg>'."\r\n";
	 		$_xml.= "\t\t".'<cidade>'.$_retorno['cidade'].'</cidade>'."\r\n";
	 		$_xml.= "\t\t".'<uf>'.$_retorno['uf'].'</uf>'."\r\n"; 
	 		$_xml.= "\t\t".'<tipo_logradouro>'.$_retorno['tipo_logradouro'].'</tipo_logradouro>'."\r\n";
	 		$_xml.= "\t\t".'<logradouro>'.$_retorno['logradouro'].'</logradouro>'."\r\n";
	 		$_xml.= "\t\t".'<bairro>'.$_retorno['bairro'].'</bairro>'."\r\n";

	 		break;  
	   
	 	default:
	 		
	 		$_xml_.="\t\t".'<cor_msg>red</cor_msg>'."\r\n";  //-> Define Cor da mensagem
		 	$_xml.= "\t\t".'<msg>Falha ao buscar CEP!</msg>'."\r\n"; 
		 	
	 	break;  
 	}
 	$_xml.= "\t"."</cep>\r\n";

 	
 	echo $_xml;
 }
}

$_cep = new busca_cep($_GET['cep']);

?>

 

 

Ola Eu estava procurando um codigo bem simples para pesquisa de cep e encontrei este

que esta muito bom para ser usado eu sou novato em php e ajax mas consegui resolver este probleminha. Encontrei dois um e o nome do id=cep igual ao do form input name=cep isto no IE ele entende com um so

(coisas do tio san) e tem um else perdido e so trocar o nome do input para um outro qualquer e copiar a funcao abaixo

que dei um jeito no else

 

CODE
// Popula os Campos do Formulário

function popula_cep(){

var mensagem = "teste #@?$%~";

carregando_cep('checar','Carregando');

if (ajax.readyState == 4) {

if (ajax.status == 200) {

var xml = ajax.responseXML; // - recebe XML

if (xml != null) { // - verifica se XML não está vazio

var nofilho = xml.hasChildNodes;

if (nofilho) { // - verifica se tem nos filhos

var nos = xml.getElementsByTagName('cep');

for (i = 0; i < nos.length; i++) {

if (window.ActiveXObject) {

var cor_msg = nos.childNodes[0].firstChild.nodeValue;

var msg = nos.childNodes[1].firstChild.nodeValue;

var cidade = nos.childNodes[2].firstChild.nodeValue;

var uf = nos.childNodes[3].firstChild.nodeValue;

var tipo = nos.childNodes[4].firstChild.nodeValue;

var logradouro = nos.childNodes[5].firstChild.nodeValue;

var bairro = nos.childNodes[6].firstChild.nodeValue;

}else{

var cor_msg = nos.childNodes[1].firstChild.nodeValue;

var msg = nos.childNodes[3].firstChild.nodeValue;

var cidade = nos.childNodes[5].firstChild.nodeValue;

var uf = nos.childNodes[7].firstChild.nodeValue;

var tipo = nos.childNodes[9].firstChild.nodeValue;

var logradouro = nos.childNodes[11].firstChild.nodeValue;

var bairro = nos.childNodes[13].firstChild.nodeValue;

}

}

var mensagem = '<font color="'+cor_msg+'">'+msg+'</font><br>';

mensagem+= tipo+': '+logradouro+'<br>';

mensagem+= 'Bairro: '+bairro+'<br>';

mensagem+= 'Cidade: '+cidade+' - UF:'+uf+'<br>';

}

}

}else{

var mensagem = 'erro na conexão com o servidor \\n'+ajax.statusText;

}

carregando_cep('checar',mensagem);

}

}

Mas muito bom este codigo Parabens vai me ajudar muito valeu.........

Compartilhar este post


Link para o post
Compartilhar em outros sites

=D Obrigado e ja q ele está arrumado então vamos subir o tópico para q este vá para o lab de scripts!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.