Search the bank in real time

Asked

Viewed 485 times

3

How can I do in PHP so that when the user type in the search field it already appears the database records.

For example, assuming that it is a register of cities, the user type "Field", should appear the records "Campo Grande, Campo Largo, Campo Mourão..." immediately, at the time of typing.

It’s too complex to do that?

  • 1

    What you want is AJAX

  • 1

    You need to use Ajax. https://www.w3schools.com/js/js_ajax_php.asp

  • And how to make the search result a dropdown and user click the result to fill the field?

1 answer

4


You can call the result via ajax and create an autocomplete list using the component Vanilla Javascript Autocomplete

You will give the autocomplete on the return of the call in Javascript:

function ShowModalRelMapaAeronave() {
    $.ajax({
        type: 'POST',
        url: BASE_SITE + '/Projeto/Projeto/BuscarAeronaves',
        data: {  },
        success: function (data) {

            new autoComplete({
                selector: '#TxtAeronave',
                minChars: 2,
                source: function (term, suggest) {
                    term = term.toLowerCase();
                    var choices = data;
                    var matches = [];
                    for (i = 0; i < choices.length; i++)
                        if (~choices[i].toLowerCase().indexOf(term)) matches.push(choices[i]);
                    suggest(matches);
                }
            });

            $('#modalRelMapaAeronave').modal('show');
        }
    });
}

Here in case I call the post in a route, I take the return and put in a txt of my html, so when it goes typing will autocomplete. You need to add javascript and css from the library.

The route is responsible for making the query.

  • +1 by lib, this breaks a good branch

Browser other questions tagged

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