Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/08/2018, 10:43
madison_sg
 
Fecha de Ingreso: noviembre-2015
Mensajes: 77
Antigüedad: 8 años, 5 meses
Puntos: 2
Llenar campos de codigo postal con ajax y javascript

Hola a todos,
__________________________________________________ ________________
NOTA IMPORTANTE: En forosdelweb, encontré algo parecido a lo que busco pero no me sirvió, no pude echar a andar ese código y por eso abro esta pregunta.
Por otra parte, NO se trabajar con Ajax, es por ello que pido ayuda.
__________________________________________________ ________________
Quiero pedirles ayuda para crear una función en ajax y javascript para que al momento de llenar el código postal de un domicilio automáticamente arroje el estado, ciudad, municipio y la colonia (ésta última en un select-option).
Hice un código en PHP para ésta función y anda muy bien, el problema es que cada vez que se consulta el código postal se refresca la página completa y eso es muy incómodo para el usuario.

Dejo mi código para ver si alguien me puede ayudar:
HTML:
Código HTML:
Código Postal:    <td width="335"><input type="text" value="<?php echo $registro_cp->codigo_postal;?>" style="width: 50px;" placeholder="" id="codigo_postal" name="codigo_postal" minlength="5" maxlength="5" onkeypress="return justNumbers(event)" /></td>
			<td width="100"><button id="consultar" class="btn btn-primary" name="consultar" onclick="return validarCP();">...</button></td>
			Estado:    <td width="335"><input type="text" value="<?php echo $registro_cp->estado;?>" placeholder="" id="estado" name="estado" readonly="readonly"/></td>
			Municipio:    <td width="335"><input type="text" value="<?php echo $registro_cp->municipio;?>" placeholder="" id="municipio" name="municipio" readonly="readonly"/></td>
			Ciudad:    <td width="335"><input type="text" value="<?php echo $registro_cp->ciudad;?>" placeholder="" id="ciudad" name="ciudad" readonly="readonly"/></td>
			Colonia   :    <td width="335"><select name="colonia" id="colonia" style="width:200px;">
				<option value="<?php echo $registro_cp->colonia;?>"><?php echo $registro_cp->colonia;?></option>
				<?php 

				 	while ($fila=mysqli_fetch_row($sql_query)) {
				 		echo "<option value='".$fila['7']."'>".$fila['7']."</option>";
				 	}
				  ?>
			    </select> 
Este es el código PHP
Código PHP:
<?php 

require('conexion.php');

try{
if(isset(
$_POST['consultar'])){  

            
$cp=$_POST['codigo_postal'];  
            
$id=$_POST['id_vdi'];//nuevo

            
$sql_querymysqli_query($con"SELECT codigo_postal, estado, municipio, ciudad, area_metropolitana,
            brick_ims, brick_atv, colonia FROM sepomex WHERE codigo_postal='$cp'"
); 
            
//
            
if(mysqli_num_rows($sql_query)<= 0){
        echo 
"<script> alert('Código postal incorrecto');</script>";
        
           
        }
            
$consultacp mysqli_query($con"SELECT * FROM tabla1 where id_vdi='$id'");//nuevo
            

            
$registro_cp mysqli_fetch_object($sql_query);     
            
$registro mysqli_fetch_object($consultacp);//nuevo
            
        
    
}
}catch (
Exception $e) {
            
        }

 
?>
__________________
paco alonso