Search content according to filling two Combobox

Asked

Viewed 569 times

0

On the home page of my site I need to have two combobox for the user to select options coming from the database (for example: selects the state in the first combobox and when selecting the state in the second combobox the cities of this state are loaded. So far, ok.). I’m finding it difficult to do the following: After the user fills the two combobox (choose state and city), I need that on this same screen, below the combos, a text appears according to the options selected. This content will come from the bank and will be according to the status id and selected city id.

My select in HTML is populated and at each selected state a Javascript function calls a file called listingCidades.php which will fill the other combobox. Ok. How to get the status id and city id and query using these two information to bring content to the same home screen?

  • Put some existing code of the party in question, to facilitate the answer and better present your doubts. From what I understand you just want to get the id and status, this would give you to do with javascript to get the necessary id’s and using ajax to send to file called listaCidades.php, in this file you return a JSON and with JS you can dynamically assemble the text accordingly.

  • Friend, in this case I believe you have the complete information (state/city) after the user select a value in the city combobox is not even ? Then you can listen to the onchange event of the city combobox for when it is changed, make a request to the server passing the selected values of the two combobox. When you post your code it becomes easier to tell which changes you need to make.

1 answer

2


How to take the status id and city id and query using these two information to bring content to the same home screen?


It is possible to do this with AJAX.

Here come the steps:

  • First combobox the state and fill in the database states via PHP loop or use a framework such as Angularjs.

  • In the "onchange" event of the combobox, put to execute a Javascript function. Ex: onchange="Loading(this.value)".

The "this.value" parameter automatically picks up the value of the selected state.

  • Create the Javascript function mentioned in the example - "Load Cities(idState)" - and, within it, place an AJAX call that will execute PHP functions/methods and return an array containing the cities. Example of AJAX:

    $.ajax({
      url:'paginaAlvo.php',
      type:'GET',
      data: {id: idEstado},
      success:function(data){
        // Preenche o combobox das cidades
      }
    });
    

Note: to know how to query in PHP and receive it in AJAX array form, see: https://stackoverflow.com/questions/10220642/pass-php-array-into-javascript-array.



If you want a more complete tutorial, search the Internet. To make it easier, I list here some nice:

http://www.marcelokenji.com.br/carregar-combobox-de-cidadeestado-com-ajaxphpmysql/

http://www.devfuria.com.br/javascript/forms/select-combobox/

Browser other questions tagged

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