Jquery autocomplete

Asked

Viewed 382 times

1

How can I make a jQuery autocomplete to pull data from a database?

I tried to use this example only I’m not getting it.

My system is in ASP.NET MVC and I am using SQL Server as a database.

  • Tried how? What problem are you having? There is no way to guess what you need...

  • @jbueno have problem when making the connection to my database.

  • So your question is how to connect to the SQL server database with Javascript, @Leonardomacedo?

  • @user7393973 yes how to make the connection joining with the autocomplete, I have a field in my form, that when the user goes typing he has to go informing the data that already has in the database, da para entender mais ou menos ?

  • @Leonardomacedo, this is not easy or quick to arrange, you have to add an event oninput to your text box that uses AJAX to connect to the database with Php, check the data, resume the correct if it is validated and then still use some Javascript to make the behavior of such auto complete. You better ask more exact questions on a problem every time you have one, or else you won’t be very lucky to have someone create a good example of code with all that working for you. Start by seeing how AJAX connects HTML/Javascript to Php without updating the page.

  • @user7393973 then, I found one more example https://jqueryui.com/autocomplete/ the way this script is it works, on my system, only in the availableTags part I wanted to pull the database, I started working in the area recently not have much experience with javascript

  • @Leonardomacedo, have you got the database done? What’s her name, table and column with the values to be taken?

  • For this example of the link in the question to work you have to change search.php to your aspnet script, read the 'term' variable sent via get and return a json with an array of strings (the bird names) filtered with the parameter sent in the term variable that is sent from the second character typed.

  • @user7393973 ja sim tenho table dbo.basics_Hospital is a column only called "hospital" <- string type because they are hospital names

  • @Antonioalexandre Thanks For your help Antonio, I’ll be taking the test now!

Show 5 more comments

1 answer

0

It’s not that easy, but it’s not a big deal either:

Page where the input will be:

 <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <title>Auto Complete - SQL</title>

            <link href="http://jquery.bassistance.de/autocomplete/jquery.autocomplete.css" type="text/css" rel="stylesheet" />
            <script src="http://code.jquery.com/jquery-1.5.2.min.js" language="javascript"></script>
            <script src="http://jquery.bassistance.de/autocomplete/jquery.autocomplete.js" language="javascript"></script>

            <script type="text/javascript" language="javascript">

                $(document).ready(function(){
                    //Ao digitar executar essa função
                    $("#nome").focus().autocomplete("exemplo02_nomes.asp",{
                        minChars: 1 //Número mínimo de caracteres para aparecer a lista
                      , matchContains: true //Aparecer somente os que tem relação ao valor digitado
                      , scrollHeight: 220 //Altura da lista dos nomes
                      , selectFirst: true //Vim o primeiro da lista selecionado
                      , mustMatch: true //Caso não existir na lista, remover o valor
                      , delay: 0 //Tempo para aparecer a lista para 0, por padrão vem 200
                      });
                });

            </script>
        </head>

        <body>
            <h1>Autocomplete com Jquery em ASP - Exemplo 2</h1>
            Nome: <input name="nome" id="nome" type="text" autocomplete="off" />
        </body>
    </html>

Page where the database settings will go:

 <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <%
        ' Parametros de Conexao ao Banco
        SERVIDOR_DO_BANCO = "localhost"
        NOME_DO_BANCO = "agenda"
        USUARIO_DO_BANCO = "mateus"
        SENHA_DO_BANCO = "123456"

        ' Conexao com o Banco de dados
        Dim conexao
        Set conexao = Server.CreateObject("ADODB.Connection")
        conexao.Open = "Provider=MSDASQL;Driver={SQL Server};Server="&SERVIDOR_DO_BANCO&";Database="&NOME_DO_BANCO&";UID="&USUARIO_DO_BANCO&";PWD="&SENHA_DO_BANCO&";" 'Efetua a Conexão

        valor = Replace(Request.QueryString("q"),"'","") 'Parâmetro do campo nome, o "q" é padrão do componente autocomplete

        ' SQL de pesquisa
        sql = "SELECT codigo, nome FROM contatos where nome like '"&valor&"%' ORDER BY nome ASC"
        Set query = conexao.execute(sql)
        While Not query.eof
            ' Lista o nome
            response.write query("nome")&"|"&query("codigo")&vbCrLf
            query.movenext
        Wend
        Set query = Nothing
        Set conexao = Nothing
    %>

Try there and see if it meets your needs, adapt, change, use as an example, the way you think best.

  • Thanks For the help Paul, I’ll be testing now too

  • only that this script is in VB right ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.