Ver Mensaje Individual
  #7 (permalink)  
Antiguo 05/08/2008, 11:29
Avatar de Superlinux
Superlinux
 
Fecha de Ingreso: octubre-2005
Ubicación: Colombia
Mensajes: 662
Antigüedad: 18 años, 7 meses
Puntos: 1
Respuesta: Select dependientes con Ajax

GatorV esta son las pruebas realizadas

Prueba.php
Código PHP:
<HTML><HEAD>
<script language="JavaScript">
function nuevoAjax()

    /* Crea el objeto AJAX. Esta funcion es generica para cualquier utilidad de este tipo, por
    lo que se puede copiar tal como esta aqui */
    var xmlhttp=false; 
    try 
    { 
        // Creacion del objeto AJAX para navegadores no IE
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    }//Fin del try
    catch(e)
    { 
        try
        { 
            // Creacion del objet AJAX para IE 
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        }//Fin del try
        catch(E) { xmlhttp=false; }
    }//Fin del catch(e) 
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest(); } 

    return xmlhttp; 
}//Fin function nuevoAjax()


function cargaContenidoCuentas()
{
    var valor=document.getElementById("select_0").options[document.getElementById("select_0").selectedIndex].value;
    if(valor==0)
    {
        // Si el usuario eligio la opcion "Elige", no voy al servidor y pongo todo por defecto
        combo=document.getElementById("select_1");
        combo.length=0;
        var nuevaOpcion=document.createElement("option"); nuevaOpcion.value=0; nuevaOpcion.innerHTML="Seleccione...";
        combo.appendChild(nuevaOpcion);    combo.disabled=true;
    }//Fin del if
    else
    {
        ajax=nuevoAjax();
        ajax.open("GET", "../PUBLIC/SelectDependientesProcesoCuentas.php?seleccionado="+valor, true);
        ajax.onreadystatechange=function() 
        { 
            if (ajax.readyState==1)
            {
                // Mientras carga elimino la opcion "Elige codcuenta" y pongo una que dice "Cargando"
                combo=document.getElementById("select_1");
                combo.length=0;
                var nuevaOpcion=document.createElement("option"); nuevaOpcion.value=0; nuevaOpcion.innerHTML="Cargando...";
                combo.appendChild(nuevaOpcion); combo.disabled=true;    
            }//Fin del if
            if (ajax.readyState==4)
            { 
                document.getElementById("fila_2").innerHTML=ajax.responseText;
            }//Fin del if
        }//Fin function() 
        ajax.send(null);
    }//Fin del else
}//Fin function cargaContenido()
</script>
</HEAD>
<BODY bgColor=#a2aace>
<table border="1" width="23%" style="border-style:none;">
  <tr valign="middle"> 
    <td width="44%" height="44" class="punteado" id="fila_1"> 
     <?php
        
//conexion BD
        // +-----------------------------------------------------------------------------------------------------+
        // Funcion para conectarme a la Base de Datos Mysql
            
function conectar() { 
              if (!(
$link=mysql_connect("localhost","root","cenco"))) { 
                  echo 
"Error conectando a la base de datos."
                  exit(); 
               } 
//Fin del if
              
if (!mysql_select_db("sigdoc",$link)) { 
                  echo 
"Error seleccionando la base de datos."
                  exit(); 
               } 
//Fin del if 
               
return $link
             } 
//Fin funcion conectar
             
$link=conectar(); 
        
?>
        <?php
        $consulta
=mysql_query("SELECT * FROM cuentas order by codcuenta");
        
?>
        <select class='combo' id='select_0' name='codcuenta' onChange='cargaContenidoCuentas()' style="background:#ffffff" onFocus="encender(this)" onBlur="apagar(this)" validationhint="required" validationmsg="El campo Cuenta es de tipo obligatorio" onKeyPress="return tabular(event,this)">
        <?php
        
        
// Voy imprimiendo el primer select compuesto 
        
while($registro=mysql_fetch_row($consulta))
        {
            echo 
"<option value='".$registro[0]."'>".$registro[0]."</option>";
        }
//Fin del while
        
?>
        </select>
    </td>
    <td width="56%" class="punteado" id="fila_2"><select class="combo" disabled="disabled" id="select_1" name="nomcuenta">
        <option value="">Cuenta...</option>
      </select></td>
  </tr>
</table>
</body>
</html>
SelectDependientesProcesoCuentas.php
Código PHP:
<?php
//conexion BD
// +-----------------------------------------------------------------------------------------------------+
// Funcion para conectarme a la Base de Datos Mysql
function conectar() { 
  if (!(
$link=mysql_connect("localhost","root","cenco"))) { 
      echo 
"Error conectando a la base de datos."
      exit(); 
   } 
//Fin del if
  
if (!mysql_select_db("sigdoc",$link)) { 
      echo 
"Error seleccionando la base de datos."
      exit(); 
   } 
//Fin del if 
   
return $link
//Fin funcion conectar
$link=conectar(); 

// +-----------------------------------------------------------------------------------------------------+
function ValidaValor($parametro)
{
    return 
TRUE;
}
//Fin function validaValor($parametro)

$valor=$_GET["seleccionado"];

if(
ValidaValor($valor))
{
    
// Genero la consulta trayendo todos los nombres que correspondan al codigo de la cuenta elegida
    
$consulta=mysql_query("SELECT * FROM cuentas WHERE codcuenta='$valor'");

    
// Comienzo a imprimir el select
    
echo "<select class='combo' id='select_1' name='codcuenta'>";
    while(
$registro=mysql_fetch_row($consulta))
    {
        
// Paso a HTML acentors y ñ para su correcta visualizacion
        
$registro[0]=htmlentities($registro[1]);
        
// Imprimo las opciones del select
        
echo "<option value='".$registro[0]."'>".$registro[1]."</option>";
    }
//Fin del while            
    
echo "</select>";
}
//Fin del if
?>
al seleccionar el codcuenta 0 este me trae el valor ....

Código PHP:
<td class="punteado" id="fila_2" width="56%"><select class="combo" id="select_1" name="codcuenta"><option value="CAJA MENOR">CAJA MENOR</option></select></td
Pero necesito ingresar el mismo 0 del codcuenta seleccionado. Aunque no se que valor me trae SelectDependientesProcesoCuentas.php ...

Gracias po tu ayuda....
__________________
"La vida sigue su curso, tú toma parte de ella."