Ver Mensaje Individual
  #6 (permalink)  
Antiguo 06/09/2010, 20:51
naciostars
 
Fecha de Ingreso: enero-2009
Ubicación: Neiva, Huila
Mensajes: 196
Antigüedad: 15 años, 4 meses
Puntos: 2
Respuesta: Problema consulta ajax

Codigo HTML

Código HTML:
<div id="scontainer">
<form name="consulta" action="" onsubmit="showQuery('query.php',consulta); return false">
  <div id="search_container">
    <p><span>Cuenta:&nbsp;</span><input name="account" type="text" size="40" maxlength="50" />&nbsp;&nbsp;
    <span>Nombre:&nbsp;</span><input name="nombre" type="text" size="40" maxlength="50" />&nbsp;&nbsp;
    <input name="Submit" type="submit" value="Submit" /></p>
  </div>  
</form>
</div>
<div id="resultado"></div> 

Código Javascript:

Código HTML:
function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
      req = false;
    }
  }
}
return req;
}
 
var http = getXMLHTTPRequest();
 
function showQuery(datos,f){
        divResultado = document.getElementById('resultado');
        var url=datos+'?account='+f.account.value+'&nombre='+f.nombre.value+'&aleatorio=' + Math.random();
        http.open("GET", url);
        http.onreadystatechange=function() {
                if (http.readyState==4) {
                    if(http.status == 200) {
                        divResultado.innerHTML = http.responseText
                    }
                }
        }
        http.send(null)
}

Código PHP:

Código PHP:
<?php
// open class conection
$DB = new DBConfig();
$DB -> config();
$DB -> conn();
 
if (
$_GET['nombre'])$nombre $_GET['nombre'];
 
// Formulate Query
 
$nombre mysql_real_escape_string($nombre);
 
//$query = "SELECT * FROM empleados"; //TRABAJA A LA PERFECCION
//$query = "SELECT * FROM empleados WHERE nombres='".$nombre."'"; //NO FUNCIONA
$query sprintf("SELECT * FROM empleados WHERE nombres='%s'",$nombre); //NO FUNCIONA
 
// Perform Query
$sql mysql_query($query);
 
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$sql) {
    
$message  'Invalid query: ' mysql_error() . "\n";
    
$message .= 'Whole query: ' $query;
    die(
$message);
}else{
 
// Use result
echo "<p>Nombres - Departamento - Sueldo</p> \n";
while(
$row mysql_fetch_array($sql)){
        echo 
"<p>".$row['nombres']." - ".$row['departamento']." - ".$row['sueldo']."</p>\n";}
 
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($sql);}
 
//close connection
$DB -> close();
?>

cualquiera de las consultas sql te debe funcionar