AJAX - Buscar AJAX

 
Vista:

Buscar AJAX

Publicado por Mercedes (32 intervenciones) el 22/08/2007 10:10:45
Buenos días.

Tengo una idea y quiero llevarla a cabo, lo que pasa es que no sé si se puede realizar o es demasiado complicado.

Tengo un combo box con una lista nombres, bien pues lo que quiero hacer es la funcionalidad de buscar uno de ellos; y se me ha ocurrido tener un textfield donde el usuario según vaya introduciendo el nombre se vayan mostrando las coincidencias que haya en el combobox. No sé si me habeis entendido. Lo mismo es muy complicado y no me merece la pena, en este caso, alguien me puede dar alguna idea de cómo puedo implementar algo para la búsqueda.

Muchas gracias de antemano.

Saludos
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Buscar AJAX

Publicado por Pepe (15 intervenciones) el 22/08/2007 20:27:38
EN TU ARCHIVO HTML DEBE DE IR MAS O MENOS ASI
<html>
<head>
<script src="clienthint.js"></script>
</head>
<body>
<form>
Nombre:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>
<p>Sugerencias: <div id="txtHint"></div></p>
</body>
</html>

EN EL ARCHIVO clienthint.js AL QUE HACES REFERENCIA EN TU HTML DEBE DE CONTENER

var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML=""
return
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="gethint.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

Y POR ULTIMO EL ARCHIVO getuser.php Q HACE REFERENCIA TU ARCHIVO clienthint.js DEBE DE LLEVAR

<?php
$q=$_GET["q"];
$result1 = mysql_query("SELECT nombre FROM datos WHERE nombre='$q");

if ($row_result1 = mysql_fetch_assoc($result1))
{
echo $row_result1['nombre'];
}
else echo 'Nombre no encontrado';
?>

ESTO ES SUPONIENDO TABLA Y CAMPOS DE TU BD, SI ES Q LO TIENES EN UNA BD YA QUE SI NO PODRIAS HACER UN ARREGLO Y ESTE QUEDARIA DE LA SIGUIENTE MANERA

<?php
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";

$q=$_GET["q"];
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
If ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
echo $response;
?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Buscar AJAX

Publicado por Aldo Pulgar (1 intervención) el 03/10/2008 09:47:35
Hola Pepe,

Vi tu ejemplo y lo encontre muy bueno, pero no me funciono del todo, cuando trato de conectarme a la base de datos me da este error "mysql_fetch_assoc". No soy muy experto en el tema pero cambien el sql, agregue la conexion y aun asi no me funciona como con el array.
Esperando te encuentres muy bien te saluda.
Aldo Pulgar
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar