Ver Mensaje Individual
  #3 (permalink)  
Antiguo 06/09/2010, 20:10
Juice
 
Fecha de Ingreso: mayo-2008
Ubicación: Guadalajara
Mensajes: 10
Antigüedad: 16 años
Puntos: 0
Respuesta: Problema consulta ajax

perdon por ser tan austero en mi pregunta, pense ser objetivo te detallo mi html seguido de mi javascript y mi php, agradezco tu apoyo.

Código HTML:
Ver original
  1. <div id="scontainer">
  2. <form name="consulta" action="" onsubmit="showQuery('query.php'); return false">
  3.   <div id="search_container">
  4.     <p><span>Cuenta:&nbsp;</span><input name="account" type="text" size="40" maxlength="50" />&nbsp;&nbsp;
  5.     <span>Nombre:&nbsp;</span><input name="nombre" type="text" size="40" maxlength="50" />&nbsp;&nbsp;
  6.     <input name="Submit" type="submit" value="Submit" /></p>
  7.   </div>  
  8. </form>
  9. </div>
  10. <div id="resultado"></div>

Código Javascript:
Ver original
  1. function getXMLHTTPRequest() {
  2. try {
  3. req = new XMLHttpRequest();
  4. } catch(err1) {
  5.   try {
  6.   req = new ActiveXObject("Msxml2.XMLHTTP");
  7.   } catch (err2) {
  8.     try {
  9.     req = new ActiveXObject("Microsoft.XMLHTTP");
  10.     } catch (err3) {
  11.       req = false;
  12.     }
  13.   }
  14. }
  15. return req;
  16. }
  17.  
  18. var http = getXMLHTTPRequest();
  19.  
  20. function showQuery(datos){
  21.         divResultado = document.getElementById('resultado');
  22.         http.open("GET", datos);
  23.         http.onreadystatechange=function() {
  24.                 if (http.readyState==4) {
  25.                     if(http.status == 200) {
  26.                         divResultado.innerHTML = http.responseText
  27.                     }
  28.                 }
  29.         }
  30.         http.send(null)
  31. }

Código PHP:
Ver original
  1. <?php
  2. // open class conection
  3. $DB = new DBConfig();
  4. $DB -> config();
  5. $DB -> conn();
  6.  
  7. if ($_GET['nombre'])$nombre = $_GET['nombre'];
  8.  
  9. // Formulate Query
  10.  
  11. $nombre = mysql_real_escape_string($nombre);
  12.  
  13. //$query = "SELECT * FROM empleados"; //TRABAJA A LA PERFECCION
  14. //$query = "SELECT * FROM empleados WHERE nombres='".$nombre."'"; //NO FUNCIONA
  15. $query = sprintf("SELECT * FROM empleados WHERE nombres='%s'",$nombre); //NO FUNCIONA
  16.  
  17. // Perform Query
  18. $sql = mysql_query($query);
  19.  
  20. // Check result
  21. // This shows the actual query sent to MySQL, and the error. Useful for debugging.
  22. if (!$sql) {
  23.     $message  = 'Invalid query: ' . mysql_error() . "\n";
  24.     $message .= 'Whole query: ' . $query;
  25.     die($message);
  26. }else{
  27.  
  28. // Use result
  29. echo "<p>Nombres - Departamento - Sueldo</p> \n";
  30. while($row = mysql_fetch_array($sql)){
  31.         echo "<p>".$row['nombres']." - ".$row['departamento']." - ".$row['sueldo']."</p>\n";}
  32.  
  33. // Free the resources associated with the result set
  34. // This is done automatically at the end of the script
  35.  
  36. //close connection
  37. $DB -> close();
  38. ?>