How can I format Custom Data and display in autocomplete when source is an DB

Posted by Andres Scarpone on Stack Overflow See other posts from Stack Overflow or by Andres Scarpone
Published on 2012-10-09T20:21:05Z Indexed on 2012/10/09 21:38 UTC
Read the original article Hit count: 201

Filed under:
|
|

so I'm trying to get some info in the auto-complete widget like it's shown in the JQuery UI demo Demo, the only problem is they use a variable that they fill with the data they want to show, I instead want to access the data and the different description and stuff using a Data Base in MySQL, for this I have changed the source to use another php page that looks up the info. here is the code for the Auto-complete, I really don't understand the methods so I haven't changed it from the basic search.

This is the JS:

$(document).ready((function(){
            $( "#completa" ).autocomplete({
        source: "buscar.php",
        minLength: 1,
                    focus: function (event, ui){
                        $("#completa").val(ui.item.val);
                        return false;
                    };
        }));

This is what I have in buscar.php:

<?php

    $conec = mysql_connect(localhost, root, admin);
    if(!$conec)
    {
        die(mysql_error());
    }
    else
    {
        $bd = mysql_select_db("ve_test",$conec );
        if(!$bd)
        {
            die(mysql_error());
        }
    }

$termino = trim(strip_tags($_GET['term']));//Obtener el termino que envia el autocompletar

$qstring = "SELECT name, descripcion FROM VE_table WHERE name LIKE '%".$termino."%'";
$result = mysql_query($qstring);//Solicitud a la Base de Datos

while ($row = mysql_fetch_array($result,MYSQL_ASSOC))//Realizar un LOOP sobre los valores obtenidos
{
                $row['value']=htmlentities(stripslashes($row['name']));
                $row_set[] = $row;//build an array
}
echo json_encode($row_set);//Enviar los datos al autocompletar en codificacion JSON, Altamente Necesario.

?>

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery