joan16v

12 enero 2010

php html ajax: como crear una búsqueda en una base de datos en AJAX usando prototype

Archivado en: General — joan16v @ 17:23
Tags: , , ,

Fichero Principal (index.php por ejemplo):
——————————————————————————-

<html>
<head>

<script type=”text/javascript” src=”../js/prototype.js”></script> <!– aqui incluimos la librería javascript PROTOTYPE –>

<script type=”text/javascript”>

function buscar_destinatario(criterio) {
$(‘resultados’).innerHTML=’<img src=”/images/ajax-loader.gif”>’;
var url=’../ajax/buscar_destinatario_ajax.php’;
var param=’criterio=’+criterio;
var ajaxRequest=new Ajax.Request( url,{ method: ‘get’, parameters: param, asynchronous: true, onComplete: showResponse});
}
function showResponse(xmlHttpRequest, responseHeader) {
$(‘resultados’).innerHTML=xmlHttpRequest.responseText;
}

</script>

</head>
<body onload=”$(‘criterio’).focus()”>

<p>Introduzca criterio de búsqueda:<br />

<input autocomplete=”off” type=”text” name=”criterio” id=”criterio” onkeyup=”buscar_destinatario(this.value)” style=”margin-top: 3px; width:200px;” /></p>

<div id=”resultados”></div>

</body>
</html>

Fichero buscar_destinatario_ajax.php:
——————————————————————————-

<?php

include("conexion_a_la_base_de_datos.php");

//si viene vacio, nada
if( trim($_GET['criterio'])=="" ) { exit(0); }

$sq="select * from destinatarios where nombre like ‘%".$_GET['criterio']."%’";
$ex=mysql_query($sq);
while( $row=mysql_fetch_object($ex) ) {
?><div><?php echo $row->nombre; ?></div><?php
}

?>

Dejar un comentario »

Aún no hay comentarios.

RSS feed para los comentarios de esta entrada. URI para TrackBack.

Deja un comentario

Fill in your details below or click an icon to log in:

Logo de WordPress.com

You are commenting using your WordPress.com account. Log Out / Cambiar )

Twitter picture

You are commenting using your Twitter account. Log Out / Cambiar )

Facebook photo

You are commenting using your Facebook account. Log Out / Cambiar )

Connecting to %s

Tema Rubric. Blog de WordPress.com.

Seguir

Get every new post delivered to your Inbox.