Foros del Web » Programando para Internet » Javascript »

Problema con 4 combox dependientes porajax

Estas en el tema de Problema con 4 combox dependientes porajax en el foro de Javascript en Foros del Web. Buenas tardes estoy haciendo una aplicacion y voy a ocupar 4 combos, la manera que la estoy implementando es la siguiente: Tengo un ajax, donde ...
  #1 (permalink)  
Antiguo 31/07/2014, 11:46
 
Fecha de Ingreso: mayo-2014
Ubicación: Mexico
Mensajes: 79
Antigüedad: 9 años, 10 meses
Puntos: 2
Problema con 4 combox dependientes porajax

Buenas tardes estoy haciendo una aplicacion y voy a ocupar 4 combos, la manera que la estoy implementando es la siguiente:

Tengo un ajax, donde mando a llamar el archivo que lo genera.
Cada combo tiene su propio archivo que lo genera, y sobre ese mismo combo llamo al otro que le sigue.

Todo va bien, cada combo, que esta en archivos separados, funciona mostrando su sucesor.

Pero cuando quiero unirlos,es decir juntar el a todos en un archivo, el primer combo manda al segundo, pero cuando seleciono el segundo, no aparece el combo que sige.

Supongo que estoy implementando mal, alguna sugerencia??
  #2 (permalink)  
Antiguo 31/07/2014, 11:57
Avatar de Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 5 meses
Puntos: 977
Respuesta: Problema con 4 combox dependientes porajax

Muéstranos el código que has elaborado para ayudarte. No olvides usar el highlight respectivo.

Saludos
__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand
  #3 (permalink)  
Antiguo 31/07/2014, 12:10
 
Fecha de Ingreso: mayo-2014
Ubicación: Mexico
Mensajes: 79
Antigüedad: 9 años, 10 meses
Puntos: 2
Respuesta: Problema con 4 combox dependientes porajax

Este es el archivo principal donde quiero que se muestre mis combos, el archivo se encuentra raiz
Código ASP:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head> 
  4. <script language="JavaScript" type="text/javascript" src="funciones/ajax.js"></script>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Documento sin título</title>
  7. </head>
  8. <body>
  9. <form id="form" name="form" method="post" action="funciones/login.asp">
  10. <table width="100%" border="1">
  11.   <tr>
  12.     <td width="33%"><img src="imagenes/logocfe.jp.jpg" width="284" height="136" /></td>
  13.     <td width="33%" align="center">CONFIGURACION MATRIX</td>
  14.     <td width="33%">&nbsp;</td>
  15.   </tr>
  16.   <tr height="65">
  17.     <td>
  18.     <%
  19.          conn_string = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Port=3306;Database=agenda_matrix;Uid=root;Pwd=123456"  
  20.         Set conn = Server.CreateObject("ADODB.Connection")
  21.         conn.Open (conn_string)      
  22.          sqlString = "select idcatmatrix, idcatagencia from catmatrix where uso = '1'"
  23.          Set RSC = conn.Execute(sqlString)
  24.          rsc.MoveFirst()
  25.          Response.Write("Id del matrix en uso: " & rsc("idcatmatrix") & "<br>de la agencia: " & rsc("idcatagencia"))
  26.     %>
  27.     </td>
  28.     <td width="33%">&nbsp;</td>
  29.     <td width="33%">&nbsp; </td>
  30.   </tr>
  31.   <tr>
  32.     <td width="33%">Cambiar a:</td>
  33.     <td width="33%">&nbsp;</td>
  34.     <td width="33%">&nbsp;</td>
  35.   </tr>
  36.   <tr>
  37.     <td width="25%">
  38.     <select id="catdivision" onchange="clic()">  
  39. <%
  40. conn_string = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Port=3306;Database=agenda_matrix;Uid=root;Pwd=123456"   
  41. Set conn = Server.CreateObject("ADODB.Connection")
  42. conn.Open (conn_string)      
  43. sqlString = "select idcatdivision, division from catdivision"
  44. Set RSC = conn.Execute(sqlString)
  45. while (not rsc.EOF)
  46.     response.write("<option value=" & rsc("idcatdivision") & ">" & rsc("idcatdivision") & " - " & rsc("division") & "</option>")       
  47.     rsc.MoveNext()
  48. wend
  49. conn.close
  50. set conn = nothing
  51. %>
  52. </select>
  53. <div id="resultado"></div>
  54. <div id="resultado1"></div>
  55.     </td>
  56.     <td width="25%"></td>
  57.     <td width="25%"></td>
  58.     <td width="25%"></td>            
  59.   </tr>
  60. </table>
  61. </form>
  62. <script>
  63. function clic(){
  64.     var division = document.getElementById("catdivision").value;
  65.     MostrarConsulta('formularios/zona.asp?division=' + division);
  66. }
  67. </script>
  68. <p>&nbsp;</p>
  69. </body>
  70. </html>

Este es el ajax, se encuentra en carpeta funciones/ajax.asp

Código Javascript:
Ver original
  1. $.ajaxSetup({cache: false});
  2. function nuevoAjax()
  3. {
  4.     var xmlhttp=false;
  5.     try
  6.     {
  7.         xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  8.     }
  9.     catch (e)
  10.     {
  11.         try
  12.         {  
  13.             xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  14.         }
  15.         catch (E)
  16.         {
  17.             try
  18.             {
  19.                 xmlhtp = new XDomainRequest();
  20.             }
  21.             catch (E)
  22.             {
  23.                 xmlhttp = false;
  24.             }
  25.         }
  26.     }
  27.     if (!xmlhttp && typeof XMLHttpRequest!='undefined')
  28.     {
  29.         xmlhttp = new XMLHttpRequest();
  30.     }
  31.     return xmlhttp;
  32. }
  33.  
  34. function MostrarConsulta(datos){
  35.     var divResultado;
  36.     divResultado = document.getElementById('resultado');
  37.     ajax=nuevoAjax();
  38.     ajax.open("POST",datos);
  39.     ajax.onreadystatechange=function() {
  40.         if (ajax.readyState==4) {
  41.             divResultado.innerHTML = ajax.responseText
  42.         }
  43.     }
  44.     ajax.send(null)
  45. }  
  46.  
  47. function MostrarConsulta1(datos){
  48.     var divResultado;
  49.     divResultado = document.getElementById('resultado1');
  50.     ajax=nuevoAjax();
  51.     ajax.open("POST",datos);
  52.     ajax.onreadystatechange=function() {
  53.         if (ajax.readyState==4) {
  54.             divResultado.innerHTML = ajax.responseText
  55.         }
  56.     }
  57.     ajax.send(null)
  58. }
  59. function MostrarConsulta2(datos){
  60.     var divResultado;
  61.     divResultado = document.getElementById('resultado2');
  62.     ajax=nuevoAjax();
  63.     ajax.open("POST",datos);
  64.     ajax.onreadystatechange=function() {
  65.         if (ajax.readyState==4) {
  66.             divResultado.innerHTML = ajax.responseText
  67.         }
  68.     }
  69.     ajax.send(null)
  70. }

Este es el 2 segundo combo, se encuentra de la carpeta raiz, formularios/zona.asp

Código ASP:
Ver original
  1. <script language="JavaScript" type="text/javascript" src="../funciones/ajax.js"></script>
  2. <form name="" method="post">
  3. <select id="catzona" onchange="clic()">  
  4. <%
  5. Dim division
  6. division = request.Querystring("division")
  7. conn_string = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Port=3306;Database=agenda_matrix;Uid=root;Pwd=123456"   
  8. Set conn = Server.CreateObject("ADODB.Connection")
  9. conn.Open (conn_string)      
  10. sqlString = "select idcatzona, zona, idcatdivision from catzona where idcatdivision = '" & division & "'"
  11. Set RSC = conn.Execute(sqlString)
  12. while (not rsc.EOF)
  13.     response.write("<option value=" & rsc("idcatzona") & ">" & rsc("idcatzona") & " - " & rsc("zona") & "</option>")       
  14.     rsc.MoveNext()
  15. wend
  16. conn.close
  17. set conn = nothing
  18. %>
  19. </select>
  20. <div id="resultado1"></div>
  21. </form>
  22. <script>
  23. function clic(){
  24.    
  25.     var zona = document.getElementById("catzona").value;
  26.     MostrarConsulta1('../formularios/agencia.asp?zona=' + zona);
  27. }
  28. </script>

Este es el 3 combo, se ubica de raiz, carpeta formularios/agencia.asp

Código ASP:
Ver original
  1. <script language="JavaScript" type="text/javascript" src="../funciones/ajax.js"></script>
  2. <form name="" method="post">
  3. <select id="catagencia" onchange="clic()">  
  4. <%
  5. Dim zona
  6. zona = request.Querystring("zona")
  7. conn_string = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Port=3306;Database=agenda_matrix;Uid=root;Pwd=123456"   
  8. Set conn = Server.CreateObject("ADODB.Connection")
  9. conn.Open (conn_string)      
  10. sqlString = "select idcatagencia, agencia from catagencia where idcatzona = '" & zona & "'"
  11. Set RSC = conn.Execute(sqlString)
  12. while (not rsc.EOF)
  13.     response.write("<option value=" & rsc("idcatagencia") & ">" & rsc("idcatagencia") & " - " & rsc("agencia") & "</option>")      
  14.     rsc.MoveNext()
  15. wend
  16. conn.close
  17. set conn = nothing
  18. %>
  19. </select>
  20. <div id="resultado2"></div>
  21. </form>
  22. <script>
  23. function clic(){
  24.    
  25.     var agencia = document.getElementById("catagencia").value;
  26.     MostrarConsulta2('../formularios/matrix.asp?agencia=' + agencia);
  27. }
  28.  
  29.  
  30. </script>

Y este es el ultimo, igual se encuentra en la carpeta formularios/matrix.asp

Código ASP:
Ver original
  1. <form name="" method="post">
  2. <select name="catmatrix">  
  3. <%
  4. Dim agencia
  5. agencia = request.Querystring("agencia")
  6. conn_string = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Port=3306;Database=agenda_matrix;Uid=root;Pwd=123456"   
  7. Set conn = Server.CreateObject("ADODB.Connection")
  8. conn.Open (conn_string)      
  9. sqlString = "select idcatmatrix, idcatagencia, uso from catmatrix where idcatagencia = '" & agencia & "'"
  10. Set RSC = conn.Execute(sqlString)
  11. while (not rsc.EOF)
  12.     response.write("<option value=" & rsc("idcatmatrix") & ">" & rsc("idcatmatrix") & "</option>")     
  13.     rsc.MoveNext()
  14. wend
  15. conn.close
  16. set conn = nothing
  17. %>
  18. </form>

Cada combo por separado, realiza su funcion, el problema es cuando lo quiero unir.
  #4 (permalink)  
Antiguo 31/07/2014, 12:39
Avatar de Alexis88
Philosopher
 
Fecha de Ingreso: noviembre-2011
Ubicación: Tacna, Perú
Mensajes: 5.552
Antigüedad: 12 años, 5 meses
Puntos: 977
Respuesta: Problema con 4 combox dependientes porajax

No he programado en ASP, pero por lo que he podido ver y entender, creo que podrías reutilizar código, usando una función para las peticiones asíncronas (Ajax) y otra para las consultas (ASP).

Algo como esto:

Código HTML:
Ver original
  1. <select id = "primero">Opciones</select>
  2. <select id = "segundo">Opciones</select>
  3. <select id = "tercero">Opciones</select>

Código ASP:
Ver original
  1. function consultas(consulta)
  2.     sqlString = consulta;
  3. end function

Código Javascript:
Ver original
  1. function ajax(id_del_combo){
  2.     //Instrucciones
  3. }

De esta manera, evitarás crear código similar una y otra vez. Cada vez que veas una estructura similar, intenta reutilizarla en lugar de repetirla.

Saludos
__________________
«Juro por mi vida y mi amor por ella, que jamás viviré para el provecho de otro hombre, ni le pediré a otro hombre que viva para el mío».

Ayn Rand
  #5 (permalink)  
Antiguo 01/08/2014, 15:24
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Problema con 4 combox dependientes porajax

intentas evaluar código traído en la respuesta para lo cual necesitas leer este articulo

por otro lado con un sólo archivo .asp te sería suficiente si usas subrutinas
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}

Etiquetas: ajax, combox, dependientes, funcion
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 14:23.