archivo conect.php
Código PHP:
if(isset($_GET['query'])) {
$q = htmlentities ($_GET['query']);
try {
$bdd = new PDO('mysql:host=host;dbname=bd', 'user', '');
$bdd->exec("set names utf8");
} catch(Exception $e) {
exit('No ha conectado.');
}
$datos = array();
$sqla1 = "SELECT objeto, codigo FROM tabla WHERE objeto LIKE '%$q%' GROUP BY objeto ORDER BY objeto ASC LIMIT 10";
$resultado = $bdd->query($sqla1) or die(print_r($bdd->errorInfo()));
while($row = $resultado->fetch(PDO::FETCH_ASSOC)) {
$datos['suggestions'][] = array("value" =>(($row['objeto'])),"codigo" => "1", "cod" => $row['codigo']);
}
echo json_encode($datos);
}
Código PHP:
<form name="form" action="sheet.php" method="POST" accept-charset="utf-8">
<input type="text" id="query" class="focus" name="search">
<input type="hidden" name="dato" id="" value=""/>
<input type="hidden" name="cod" id="" value=""/>
</form>
<div id="selection"></div>
<script type="text/javascript" src="jsValidos/jquery.autocomplete.min.js"></script>
<script type="text/javascript">
$(function(){
$('#query').autocomplete({
serviceUrl: 'lib/util/conect.php',
dataType: 'json',
minLength: 2,
select : function(event, object){
$('#suggestions').val(object.item.value);
$('#dato').val(object.item.codigo);
$('input[name="dato"]:hidden').val(object.item.codigo);
$('#cod').val(object.item.cod);
$('input[name="cod"]:hidden').val(object.item.cod);
$('#suggestions').slideDown('slow');
}});
});
</script>
saludos y gracias