Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/03/2013, 06:37
kiri_vfk
 
Fecha de Ingreso: diciembre-2012
Mensajes: 17
Antigüedad: 12 años, 3 meses
Puntos: 0
tercer nivel select dinamico

Hola necesito ayuda con AJAX ya que acabo de empezar con ello. Tengo que crear un buscador con 3 select dinamicos. He conseguido hacer que seleccionando una opcion del primero me busque en mysql las opciones para un segundo select pero no se como hacer para un tercero y para mostrar despues la informacion.

Esto es lo que he conseguido hasta ahora:

http://www.goodfly.es/buscador/ajax.php

Y este el codigo;
Código PHP:
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. <title>Buscador ajax</title>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <script>
  7. function ver_destinos(str)
  8. {
  9. if (str=="")
  10.   {
  11.   document.getElementById("txtHint").innerHTML="";
  12.   return;
  13.   }
  14. if (window.XMLHttpRequest)
  15.   {// code for IE7+, Firefox, Chrome, Opera, Safari
  16.   xmlhttp=new XMLHttpRequest();
  17.   }
  18. else
  19.   {// code for IE6, IE5
  20.   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  21.   }
  22. xmlhttp.onreadystatechange=function()
  23.   {
  24.   if (xmlhttp.readyState==4 && xmlhttp.status==200)
  25.     {
  26.     document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  27.     }
  28.   }
  29. xmlhttp.open("GET","destinos.php?q="+str,true);
  30. xmlhttp.send();
  31. }
  32. </script>
  33. </head>
  34. <body>
  35. <div id="selector" style="background-color: rgb(166, 245, 135); height:60px; border-radius:10px; padding:10px; border: 1px solid darkseagreen;">
  36.     <div id="contenido" style="float: left; font-size: 18px; font-family: 'Times New Roman', Times, serif;">  
  37.          <?php
  38.          require("conexion.php");
  39.             mysql_query("SET NAMES 'utf8'");
  40.             if (!$conexion)
  41.               {
  42.               die('Could not connect: ' . mysql_error());
  43.               }
  44.             $sql="SELECT *  FROM vuelos";
  45.             $result = mysql_query($sql);
  46.             echo "<form>";
  47.             echo '<label>Seleccione su lugar de origen</label>';
  48.             echo '<select style="margin-left:15px" name="origenes" onchange="ver_destinos(this.value)">';
  49.             echo '<option value="">Selecciona un origen</option>';
  50.             while($row = mysql_fetch_array($result))
  51.                 {
  52.                 echo "<option value=". $row['id'] .">". $row['nombre_origen'] ."</option>";
  53.                 }
  54.              echo '</select>';
  55.              mysql_close($conexion);
  56.          echo '</form>';
  57.          echo '<br>';
  58.          ?>
  59.     </div>  <!--Ciero div contenido-->
  60.     <div id="contenido2" style="float: left; font-size: 18px; font-family: 'Times New Roman', Times, serif;">  
  61.         <div style="margin-left:30px" id="txtHint"></div>
  62.     </div>
  63. </div>  <!--Cierro el bloque selector -->
  64. <div>
  65.  
  66. </div>
  67. </body>
  68. </html>

destinos.php:

Código PHP:
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. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Documento sin título</title>
  6. </head>
  7. <body>
  8. <?php
  9. $q=$_GET["q"];
  10. require("conexion.php");
  11. mysql_query("SET NAMES 'utf8'");
  12. if (!$conexion)
  13.   {
  14.   die('Could not connect: ' . mysql_error());
  15.   }
  16. $sql="SELECT DISTINCT id_destinos FROM hoteles_destinos WHERE id_origen = '".$q."'";    //busco los id de los destinos posibles
  17. $result = mysql_query($sql);
  18. echo '<label>Seleccione su destino</label>';
  19. echo '<select style="margin-left:15px" name="destinos" id="destinos">';
  20. echo '<option value="">Elige</option>';
  21. while($row = mysql_fetch_array($result))
  22.   {
  23.       $sql2 = "SELECT nombre_origen FROM vuelos WHERE id = '".$row['id_destinos']."'";  //a partir de los id busco en la tabla de destinos el nombre
  24.       $result2 = mysql_query($sql2);
  25.       while($row2 = mysql_fetch_array($result2))   
  26.         {
  27.             echo "<option value=". $row2['id'] .">". $row2['nombre_origen'] ."</option>";   //muestro los destinos posibles para ese origen
  28.         }
  29.   }
  30. echo "</select>";
  31. mysql_close($conexion);
  32. ?>
  33. </body>
  34. </html>