Find content according to two Combobox

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: select the state in the first combobox and when selecting the state in the second combobox are loaded the cities of this state. , OK.). I'm having trouble doing the following: After the user populates the two combobox (choose state and city), I need that in the 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 id of the state and the id of the selected city.

My select in HTML is populated and with each state selected a JavaScript function calls a file called the Cities.php list that will populate the other combobox. Ok. How do I get the id of the state and the id of the city and make an inquiry using these two information to bring content on the same home screen?

    
asked by anonymous 20.01.2016 / 17:01

1 answer

2
  

How to get the id of the state and the id of the city and make a query using these two information to bring content on the same home screen?

You can do this with AJAX.

Here are the steps:

  • First make the state's combobox and fill in the states of the database via the PHP loop or use a framework, such as AngularJS.

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

The "this.value" parameter automatically takes the value of the selected state.

  • Create the JavaScript function mentioned in the example - "LoadClients (idEstado)" - and, inside it, put an AJAX call that will execute PHP functions / methods and return an array containing the cities. AJAX Example:

    $.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 as an array, see: link .

  • Now, make AJAX populate the cities combobox. See: link .

If you want a more complete tutorial, search the Internet. To make it easier, here are some cool things:

link

link

    
21.01.2016 / 12:31