Foros del Web » Programando para Internet » PHP »

Consultar BD con PHP y AJAX

Estas en el tema de Consultar BD con PHP y AJAX en el foro de PHP en Foros del Web. Buenos días, tengo un problema con PHP i AJAX, os cuento. Tengo una página web con 3 desplegables. En función de lo que escojo en ...
  #1 (permalink)  
Antiguo 17/09/2014, 04:30
 
Fecha de Ingreso: noviembre-2010
Mensajes: 24
Antigüedad: 13 años, 5 meses
Puntos: 0
Consultar BD con PHP y AJAX

Buenos días,

tengo un problema con PHP i AJAX, os cuento.

Tengo una página web con 3 desplegables. En función de lo que escojo en el primero, me tienen que salir unas opciones en el segundo, y en función de lo que escoja en el segundo me tienen que salir unas opciones en el tercero. Estas opciones las consulto en mi base de datos y eso me funciona correctamente.
El orden de los ficheros es el siguiente:
1 -> 2 -> 3
html_ejemplo.php -> get_user.php -> get_user2.php

Os los copio a continuación:

html_ejemplo.php
Código PHP:
Ver original
  1. <html>
  2. <head>
  3. <script>
  4. function showUser(str, str2) {
  5.   if (str=="") {
  6.     document.getElementById("txtHint").innerHTML="";
  7.     return;
  8.   }
  9.   if (window.XMLHttpRequest) {     
  10.     xmlhttp=new XMLHttpRequest();   // code for IE7+, Firefox, Chrome, Opera, Safari
  11.   } else {                         
  12.     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
  13.   }
  14.   xmlhttp.onreadystatechange=function() {
  15.     if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  16.       document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  17.     }
  18.   }
  19.   xmlhttp.open("GET","get_user.php?q="+str+"&q2="+str2,true);
  20.   xmlhttp.send();
  21. }
  22. </script>
  23. </head>
  24. <body>
  25.  
  26. <form>
  27. <select name="users" onchange="showUser(this.value, 3)">
  28. <option value="">Escoger:</option>
  29. <option value="1">Proyecto 1</option>
  30. <option value="2">Proyecto 2</option>
  31. <option value="3">Proyecto 3</option>
  32. <option value="22">Proyecto 22</option>
  33. </select>
  34. </form>
  35. <br>
  36.     <p id="txtHint">-</p>
  37. </body>
  38. </html>

get_user.php
Código PHP:
Ver original
  1. <html>
  2. <head>
  3. <script>
  4. function showUser2(str, str2) {
  5.   if (str=="") {
  6.     document.getElementById("txtHint").innerHTML="";
  7.     return;
  8.   }
  9.   if (window.XMLHttpRequest) {    // code for IE7+, Firefox, Chrome, Opera, Safari
  10.     xmlhttp=new XMLHttpRequest();
  11.   } else { // code for IE6, IE5
  12.     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13.   }
  14.   xmlhttp.onreadystatechange=function() {
  15.     if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  16.       document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
  17.     }
  18.   }
  19.   xmlhttp.open("GET","get_user2.php?q3="+str+"&q4="+str2,true);
  20.   xmlhttp.send();
  21. }
  22. </script>
  23. </head>
  24. <body>
  25.  
  26. <?php
  27. header('Content-Type: text/html; charset=ISO-8859-1');
  28. require("connBD.php");
  29.  
  30. $id = intval($_GET['q']);
  31. $q2 = intval($_GET['q2']);
  32.  
  33. $sqla = "select ... ...'";
  34. $resultado = mysql_query($sqla);
  35.  
  36. echo "q2: ".$q2;
  37.  
  38. echo '<select name="resul_'.$q2.'" id="resul_'.$q2.' onchange="showUser2(this.value, 3)">';
  39.  
  40. while($rowa = mysql_fetch_assoc($resultado)) {
  41.     $id = $rowa['id'];
  42.     echo "<option id=".$id.">".$rowa['nom']."</option>";
  43. }
  44. echo '</select>';
  45.  
  46. ?>
  47. <p id="txtHint">-</p>


get_user2.php
Código PHP:
Ver original
  1. <?php
  2. header('Content-Type: text/html; charset=ISO-8859-1');
  3. require("connBD.php");
  4.  
  5. $id2 = intval($_GET['q3']);
  6. $q4 = intval($_GET['q4']);
  7.  
  8. $sqla = "select ..."'";
  9. $resultado2 = mysql_query($sqla);
  10.  
  11. echo "q4: ".$q4;
  12.  
  13. echo '<select name="resul_'.$q4.'" id="resul_'.$q4.'">';
  14.  
  15. while($rowa = mysql_fetch_assoc($resultado2)) {
  16.     $id_3 = $rowa['id'];
  17.     echo "<option id=".$id_3.">".$rowb['nom_2']."</option>";
  18.     }
  19. }
  20. echo '</select>';
  21.  
  22. ?>

Entonces,... el funcionamiento de 1 a 2 va bien, pero de 2 a 3 no. Si modifico algunos parámetros y pruebo de 1 a 3 también me funciona correctamente.
Las variables q2 y q4 están ahí porqué este trozo de código, cuando funcione, tiene que ir dentro de un bucle ya que tengo varios desplegables en una misma página.

Alguien puede ver el error?

Muchas gracias!!

Etiquetas: ajax, bd, consultar, html, mysql, select, sql, variable
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 04:35.