Ver Mensaje Individual
  #4 (permalink)  
Antiguo 16/03/2009, 08:08
Avatar de farra
farra
 
Fecha de Ingreso: marzo-2008
Ubicación: Aqui estoy
Mensajes: 574
Antigüedad: 16 años, 1 mes
Puntos: 20
Respuesta: select enlazados (one moret time)

Espero que este ejemplo de select entrelazados usando ajax y php te sirva...




Crea 4 archivos:


1) archivo selectentrelazados.php:

Código PHP:
<?php require_once('Connections/area.php'); ?>
<?php
mysql_select_db
($database_area$area);
$query_Recordset1 "SELECT Iddepartamento, departamento FROM departamento order by departamento ASC";
$Recordset1 mysql_query($query_Recordset1$area) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);
?><!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" />
<title>Documento sin t&iacute;tulo</title>
<script type="text/javascript" language="javascript">
<!--//
function toggle(what) {
        var aobj = document.getElementById(what);
        if( aobj.style.display == 'none' ) {
               aobj.style.display = '';
        } else {
               aobj.style.display = 'none';
        }
}
function CrearXMLHttp(){
    XMLHTTP=false;
    if(window.XMLHttpRequest){
        return new XMLHttpRequest();
    }else if(window.ActiveXObject){
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}
// suponiendo que tu div se llama loading
function OpenPage(url,id,loading){
//alert(url+' '+id);
    req=CrearXMLHttp();
    if(req){
        req.onreadystatechange = function() { manejador(id,loading); }; // aca cambie
        req.open("POST",url,true);
        req.send(null);
                toggle(loading); // ojo aqui
    }
}
function manejador(id,loading){
    if(req.readyState == 4){
        if(req.status == 200){
                        toggle(loading); // ojo aca
            document.getElementById(id).innerHTML=req.responseText;
        }else{
            //alert("Error"+req.statusText)
            alert("Error: es posible que tu navegador no sea compatible con las funciones de esta pagina, proba ingresando de nuevo desde Internet Explorer.");
        }
    }
}

function cargaselect(){
var variable = document.getElementById('depto').value;
var url = 'select2.php?id='+variable;

        OpenPage(url,'ciudades','loading');
}
</script>
</head>

<body>
<div id="provincias">
  <label>
  <select name="select1" id="depto" onchange="cargaselect();">
  <?php do{ ?>
      <option value="<?php echo $row_Recordset1['Iddepartamento']; ?>"><?php echo $row_Recordset1['departamento']; ?></option>
    <?php } while ($row_Recordset1 mysql_fetch_assoc($Recordset1)); ?>
  </select>
  </label>
</div>
<br />
<br /><br />
<div id="ciudades">
     <?php require("cargando.php"); ?> 
</div>
</body>
</html>
<?php
mysql_free_result
($Recordset1);
?>
2) archivo select2.php:

Código PHP:
<?php require_once('Connections/area.php'); ?>
<?php
$id
=intval($_GET['id']);
mysql_select_db($database_area$area);
$query_Recordset2 "SELECT Idciudad, Iddepartamento, Ciudad FROM ciudad Where Iddepartamento = '$id'";
$Recordset2 mysql_query($query_Recordset2$area) or die(mysql_error());
$row_Recordset2 mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 mysql_num_rows($Recordset2);
?>
<select name="ciudad">
<?php do{ ?>
    <option value="<?php echo $row_Recordset2['Idciudad']; ?>"><?php echo htmlentities($row_Recordset2['Ciudad']); ?></option>
<?php } while ($row_Recordset2 mysql_fetch_assoc($Recordset2)); ?>
</select>
<?php require('cargando.php'); ?>
<?php
mysql_free_result
($Recordset2);
?>

3) archivo cargando.php:

Código PHP:

<br /><div id="loading" style="display:none; background-color:#FF0000; color:#FFFFFF; font-weight:bold;  width:120px;">Cargando...</div><br /> 

4) archivo area.php:


Código PHP:

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_area "localhost";
$database_area "area";
$username_area "root";
$password_area "";
$area mysql_pconnect($hostname_area$username_area$password_area) or trigger_error(mysql_error(),E_USER_ERROR); 
?>
__________________
Firma:
Es mas dificil para el mono entender que el hombre desciende de el....

PD: Siempre doy karma al que me da una buena respuesta... ;0)