CENTRAL DO CLIENTE   
(usehost) Base de Conhecimentos

Home | Sumário | Favoritos | Contato | Efetuar Login Home | Sumário | Favoritos | Contato | Efetuar Login
Pesquisar na base de dados Pesquisar por categoria
AutoComplete jQuery
Detalhes do artigo

Última atualização
11th o June, 2010

Opções de usuário (1 voto)
100% thumbs up 0% thumbs down

Como você avaliaria este artigo?
Útil
Inútil
I’ve been working on a lot of JavaScript and PHP projects at work recently which is one of the reasons why I haven’t written a post for the last week. During my many projects i had the need to use an ‘autosuggest’ script that would, in my case, get a list of 10 schools based on the users input in a text field. I thought I’d share this with you as it’s probably easier than you think to do with JavaScript.

Lets run through the JavaScript.

There are two functions in the block of code below. the first one ’suggest’ performs the ajax request based on the users input into the text field which is passed into the function when the function is run. We reference the input with the variable ‘inputString’.

the first section of code is a simple if statement to check if the length of the users input in the text box is more than 0. If it is we run the ajax request to get the list of suggestions from the database. #country refers to the id of the text input in our form. You can see that we’re adding the class ‘load’ to the text input. This is to display an animated loading gif to show the user that we’re getting data from the database.

We then use ‘$.post’ to post the users input text to the ‘autosuggest.php’ page for processing. The autosuggest.php page simple returns a result based on a LIKE % sql query.

Once we have data back from the ‘autosuggest.php’ file, we fade in the suggestion box and inject the contents into the ‘#suggestionsList’ div using ‘.html’. We finally remove the ‘load’ class which removes the animated loading gif.

The function ‘fill()’ populates the text input field with the users selection if they click in a country from the suggested list, it then fades out the ‘#suggestions’ div removing it from the page.


function suggest(inputString){
if(inputString.length == 0) {
$('#suggestions').fadeOut();
} else {
$('#country').addClass('load');
$.post("autosuggest.php", {queryString: ""+inputString+""}, function(data){
if(data.length >0) {
$('#suggestions').fadeIn();
$('#suggestionsList').html(data);
$('#country').removeClass('load');
}
});
}
}

function fill(thisValue) {
$('#country').val(thisValue);
setTimeout("$('#suggestions').fadeOut();", 600);
}

The HTML.

This is the HTML needed to get the autosuggest feature working. Its a simple form, with a text input with an onKeyup and onBlur attribute. The suggestion box with arrow is situated below the form input and positioned absolutely relative to the div suggest.


 <form id="form" action="#">
<div id="suggest">Start to type a country: <br />
<input type="text" size="25" value="" id="country" onkeyup="suggest(this.value);" onblur="fill();" class="" />

<div class="suggestionsBox" id="suggestions" style="display: none;"> <img src="arrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="suggestionsList">   </div>
</div>
</div>
</form>

Included in the download package is the database .sql file needed to
create the table to search from. You could however modify the SQL query
to point to a different table. The database connection details are also
declared at the top of the autosuggest.php page however it is advised to abstract these from the page.


demodownload


Comentários
Não hpa comentários de visitantes. Comentar
Postou comentário para o artigo "AutoComplete jQuery"
Para postar um comentário para este artigo, preencha o formulário a seguir. campos marcados com asterisco são obrigatórios.
   Seu nome:
   Endereço de e-mail:
* Comentário:
* Digite o código a seguir::
 
Artigos relacionados
Não foi encontrado artigos relacionados.
Anexos
Nenhum anexo.

Continuar

Pagamento de Faturas
2ª Via do Boleto Bancário
Digite o N° da Cobrança:

 

Se seu pagamento não for por
boleto bancário, acesse a nossa
Central do Cliente. E proceda o pagamento.

UMA EMPRESA DO GRUPO INTECORP TECNOLOGIA®
(C) Copyright 2004-2010 (usehost) Serviços de Internet do Brasil.
Compartilhe: