Search in the bank from option [closed]

2

I need that when selecting an option in a option , a search in the database without leaving the page and showing what was returned through this search and filling fields with the results. I already have the layout mounted and the script for the bank, but I do not know how to do it when an option is selected in the option .

    
asked by anonymous 12.04.2017 / 21:09

1 answer

1

There is no exact answer to your question

As the @rray comment says, you will need AJAX .

What is AJAX?

AJAX is a technology that makes it possible to make a request to the server without having to refresh the page, which I assume is exactly what you want to do.

How do I use AJAX?

Well, it's relatively simple. This type of request exists in the browser through xmlHttpRequest , which works something like this:

var xhttp = new XMLHttpRequest();
//Essa parte é executada quanddo a requisição retornou do servidor
xhttp.onreadystatechange = function() {
    //Verifica se a requisição foi um sucesso
    if (this.readyState == 4 && this.status == 200) {
       // Aqui é aonde você faz as coisas que você precisa depois que contatou o servidor;
    }
};

//Parametro 1 -> Nome do método a ser chamado no servidor
//Parametro 2 -> Nome da página aonde está o parâmetro 1
//Parametro 3 -> Define se é síncrono ou assíncrono
//É nesse método que você efetivamente FAZ requisição
xhttp.open("GET", "filename", true);
xhttp.send();

In case, I know you have a option , so you could do something like: (note that my examples are generic since I can not know your code, or what you want precisely)

document.getElementByID('seuElemento').onchange = function (){
    requisicaoAjax();
}

function requisicaoAjax(){
    var xhttp = new XMLHttpRequest();

    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
           alert('Ei, foi um sucesso!');
        }
    };
    xhttp.open("GET", "pagina", true);
    xhttp.send();
}

Maybe you've heard of the JQuery library (I'm not saying you should just use it). It encapsulates xmlHttpRequest , and gets really cute, look:

$.ajax({
    type: "POST",
    url: 'pagina/metod',
    data: 'seusParametros',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        //foi um sucesso!
    },
    error: function (msg) {
        //deu pau, trate seu erro aqui...
    }
});

Anyway, this is just a tiny introduction. I recommend you read this

    
12.04.2017 / 21:33