Ver Mensaje Individual
  #16 (permalink)  
Antiguo 11/06/2009, 04:55
Avatar de chiquirf
chiquirf
 
Fecha de Ingreso: noviembre-2005
Ubicación: Madrid
Mensajes: 215
Antigüedad: 18 años, 5 meses
Puntos: 3
Respuesta: validar campo con datos de Base de datos

Pues funcionó a las mil maravillas, muchas gracias abimaelrc...

Este es el cod por si a alguien le interesa:

Código PHP:
var xmlHttp;
function 
showPoll(VARIABLE){
    
xmlHttp=GetXmlHttpObject();
    if (
xmlHttp==null){
        
alert ("Tu navegador no soporta AJAX! No podras usar esta aplicación.");
        return;
    }

    var 
url="codigos/codpostsearch.php";
    
url=url+"?cod_post="+VARIABLE;


    
//Puedes crear una variable en la funcion que diga cual es el id que deseas verificar tu escoges
    
document.getElementById("p3valid").value="";
    
document.getElementById("div_cp").innerHTML="";
    
xmlHttp.onreadystatechange=function(){
        
/*
        if (xmlHttp.readyState==1){
        
        }
        */
        
if (xmlHttp.readyState==4){
            
img document.createElement('img');
            if(
xmlHttp.responseText =="ok"){ img.src="img/ok.png";} //ok.png es una imagen con un checkok verde.
            
else { img.src="img/ko.png";} //ko.png es una imagen con una "X" rojas.
            
img.border=0;
            
img.width 16;
            
img.heigth 16;
            
document.getElementById("div_cp").appendChild(img);
            
            
document.getElementById("p3valid").value=xmlHttp.responseText;
            
// document.getElementById("span_cp").innerHTML=xmlHttp.responseText;
        
}
    }
    
xmlHttp.open("GET",url,true);
    
xmlHttp.send(null);
}

function 
GetXmlHttpObject(){
    var 
xmlHttp=null;
    try{
        
// Firefox, Opera 8.0+, Safari
        
xmlHttp=new XMLHttpRequest();
    }catch(
e){
        
// Internet Explorer
        
try{
            
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch(
e){
            try{
                
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }catch(
e){}
        }
    }
    return 
xmlHttp;

y el HTML

Código HTML:
<input name="p3" id="p3" type="text" maxlength="5"  onblur="javascript:showPoll(this.value);"  value="" />

<div id="div_cp"></div>

<input name="p3valid" id="p3valid" type="hidden"/>


Luego en codpostsearch.php lo unico que hace es recibir el codigo postal por GET, buscar en la BBDD y si lo encuentra hace un echo 'ok' y si no lo encuntra hace un echo 'ko' .

Luego cuando envío el formulario, valido si p3valid es OK o KO para dejar continuar o no.


Pues este es mi primer AJAX, y no ha sido muy dificil.

Gracias a todos...

Saludos

Carlos
__________________
=================
Chiqui nunca muere ! ! !