Foros del Web » Programando para Internet » Javascript »

combox list enlazados

Estas en el tema de combox list enlazados en el foro de Javascript en Foros del Web. Hola estoy trabajando con una base de datos en mysql, en mi formulario deseo colocar dos combox list enlazados uno para cada tabla, donde el ...
  #1 (permalink)  
Antiguo 09/02/2011, 07:21
 
Fecha de Ingreso: agosto-2008
Mensajes: 94
Antigüedad: 18 años, 2 meses
Puntos: 0
combox list enlazados

Hola estoy trabajando con una base de datos en mysql, en mi formulario deseo colocar dos combox list enlazados uno para cada tabla, donde el segundo combo muestre la informacion dependiendo de lo que selecciono el usuario en el primer combo, ya que es mucha informacion en las tablas no puedo colocar en el codigo la opciones. He investigado y se puede trabajar con java pero en los ejemplos colocan las opciones en el codigo y funcionan pero cuando lo adapto a lo que necestio no me funciona; adjunto el codigo que cambiado a mi necesidad. gracias por su ayuda.
Código PHP:

<html>
 <head>
  <title>
   FAQ-1: Selects
  </title>
  <script language="JavaScript">

   function addOpt(oCntrl, iPos, sTxt, sVal){
     var selOpcion=new Option(sTxt, sVal);
     eval(oCntrl.options[iPos]=selOpcion);
   }

   function cambia(oCntrl){
    while (oCntrl.length) oCntrl.remove(0);
    switch (document.frm.central.selectedIndex){
       <?php
                 
include("conexion.php"); 
                 
$conexion = obtenerConexion(); 
                 
$result1=mysql_query("select CONCAT(C4, '/ ',cSerial,'/',cRangoI,'/', cRangoF) AS Completo from tnumeracion ");
                 if (
$row1 = mysql_fetch_array($result1,$conexion)){ 
                 echo 
'<select name= "central">';
                 do {
                 echo 
'<option value= "'.$row1["Completo"].'">'.$row1["Completo"].'</option>';
                 } while (
$row1 = mysql_fetch_array($result1)); 
                 echo 
'</select>';
                 }
                 
?> 
      break;

    }
   }
  </script>
 </head>
 <body>
  <form name="frm">
   <table border="0">
    <tr>
     <td>
      Central
     </td>
     <td>
              <?php
                   $conexion 
= obtenerConexion(); 
                 
$result=mysql_query("select CONCAT(C4, '/ ',CENTRAL,'/',NUMCPA) AS nombre from tmatrizcpa ORDER BY CENTRAL DESC");
                 if (
$row = mysql_fetch_array($result,$conexion)){ 
                 echo 
'<select name= "central" onchange="cambia(document.frm.numeracion)">';
                 do {
                 echo 
'<option value= "'.$row["nombre"].'">'.$row["nombre"].'</option>';
                 } while (
$row = mysql_fetch_array($result)); 
                 echo 
'</select>';
                 }
                 
?> 
     </td>
     <td>
      &nbsp;
     </td>
     <td>
      Numeracion
     </td>
     <td>
       <?php
               
                 $conexion 
= obtenerConexion(); 
                 
$result1=mysql_query("select CONCAT(C4, '/',cSerial,'/',cRangoI,'/', cRangoF) AS Completo from tnumeracion");
                 if (
$row1 = mysql_fetch_array($result1,$conexion)){ 
                 echo 
'<select name= "numeracion">';
                 do {
                 echo 
'<option value= "'.$row1["Completo"].'">'.$row1["Completo"].'</option>';
                 } while (
$row1 = mysql_fetch_array($result1)); 
                 echo 
'</select>';
                 }
                 
?> 
     </td>
    </tr>
   </table>
  </form>
 </body>
</html>
  #2 (permalink)  
Antiguo 09/02/2011, 08:30
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 18 años, 4 meses
Puntos: 1012
Respuesta: combox list enlazados

en la faq de ajax tienes algunos como código sobre combox dependientes
  #3 (permalink)  
Antiguo 14/02/2011, 13:35
 
Fecha de Ingreso: agosto-2008
Mensajes: 94
Antigüedad: 18 años, 2 meses
Puntos: 0
Respuesta: combox list enlazados

hola a todos encontre un ejemplo como paises, ciudad y estados y lo probe y funciona, pero cuando lo adecuo a lo que necesito no me funciona; si me pueden ayudar a ver en que me equivoque porq he revisado y nada. primero colocare el original y luego el mio. gracias
Código PHP:
original formulario.php
   
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ENâ€� â€œhttp://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>

    <script language="javascript" src="js/jquery-1.3.2.min.js"></script>
    <script language="javascript">
    $(document).ready(function(){
    $("#cmbpais").change(function () {
    $("#cmbpais option:selected").each(function () {
    elegido=$(this).val();
    $.post("estados.php", { elegido: elegido }, function(data){
    $("#cmbestados").html(data);
    $("#cmbciudades").html("");
    });
    });
    })
    // Este puede ponerse en comentario si no se dispone de un 3er combo:
    $("#cmbestados").change(function () {
    $("#cmbestados option:selected").each(function () {
    elegido=$(this).val();
    $.post("ciudades.php", { elegido: elegido }, function(data){
    $("#cmbciudades").html(data);
    });
    });
    })

    });
    </script>

    <body>
    <form name="form1">
    <center>
    <table border = "1">
    <tr><td>Pais:</td><td>
    <select id="cmbpais">
    <?php
    
include 'conexion.php';
    
conectar();
    
$consulta = "select * from paises";
    
$resultado = mysql_query($consulta);
    if(
$fila=mysql_fetch_array($resultado)){
    do{
    echo 
'<option value="'.$fila['id_pais'].'">'.$fila['pais'].'</option>';
    }while(
$fila = mysql_fetch_array($resultado));
    }
    
desconectar();
    
?>
    </select></td></tr>
    <tr><td>Estado:</td><td><select id="cmbestados"></select></td></tr>
    <tr><td>Ciudad:</td><td><select id="cmbciudades"></select></td></tr>
    </table>
    </center>
    </form>
    </body>
    </html>
Código PHP:
estados.php
<?php
    
include 'conexion.php';
    
conectar();
    
$consulta = "select * from estados where id_pais = '$_POST[elegido]' order by estado";
    
$resultado = mysql_query($consulta);
    
$registros = mysql_fetch_row($resultado);
    if(
$registros){
        while(
$fila = mysql_fetch_array($resultado)){
            echo 
"<option value=".$fila['id_estado'].">".$fila['estado']."</option>";
        }
    }
    
desconectar();
?>
Código PHP:
<?php
    
include 'conexion.php';
    
conectar();
    
$consulta = "select * from ciudades where id_estado = '$_POST[elegido]' order by ciudad";
    
$resultado = mysql_query($consulta);
    
$registros = mysql_fetch_row($resultado);
    if(
$registros){
        
$resultado = mysql_query($consulta);
        while(
$fila = mysql_fetch_array($resultado)){
            echo 
"<option value=".$fila['id_ciudad'].">".$fila['ciudad']."</option>";
        }
    }
    
desconectar();
?>
en mi caso nada mas tengo dos combos lista.
Código PHP:
formulario.php
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ENâ€� â€œhttp://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    <script language="javascript" src="jquery-1.3.2.min.js"></script>
    <script language="javascript">
    $(document).ready(function(){
    $("#central").change(function () {
    $("#central option:selected").each(function () {
    elegido=$(this).val();
    $.post("numeracion.php", { elegido: elegido }, function(data){
    $("#cmbestados").html(data);
    });
    });
    })

    });
    </script>
    <body>
    <form name="form1">
    <center>
    <table border = "1">
    <tr><td>Central:</td><td>
    <select id="central">
    <?php
    
include ('conexion.php');
    
$conexion = obtenerConexion(); 
    
$consulta = "select * from tmatrizcpa1 order by id_matriz";
    
$resultado = mysql_query($consulta);
    if(
$fila=mysql_fetch_array($resultado)){
    do{
    echo 
'<option value="'.$fila['id_matriz'].'">'.$fila['CENTRAL'].'</option>';
    }while(
$fila = mysql_fetch_array($resultado));
    }
    
?>
    </select></td></tr>
    <tr><td>Rangos:</td><td><select id="central"></select></td></tr>
    </table>
    </center>
    </form>
    </body>
Código PHP:
numeracion.php
<?php
    
include 'conexion.php';
 
$conexion = obtenerConexion(); 
    
$consulta = "select * from tnumeracion1 where id_matriz = '$_POST[elegido]'";
    
$resultado = mysql_query($consulta);
    
$registros = mysql_fetch_row($resultado);
    if(
$registros){
        while(
$fila = mysql_fetch_array($resultado)){
            echo 
"<option value=".$fila['id_numeracion'].">".$fila['cRango']."</option>";
        }
    }
?>

Etiquetas: combox, list
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 06:56.