List data in a drop down related to the result of another drop down

Asked

Viewed 811 times

1

I have no idea how to proceed in this case

Summarizing accurate in 2nd dropdown list data related to the result of the first
In the case of the script below it shows the companies and users belonging to them

1º Select: List companies and get the Id of the selected

<select  name="empresa"  style="width: 160px;" required>
$sql = "SELECT * FROM empresas WHERE id_transfer = '$id_transfer'";
$resultado = mysql_query($sql) or die( mysql_error());
while ($linha = mysql_fetch_assoc($resultado)) {
$id_empresa = $linha["id_empresa"];
$nome = $linha["nome_fan"];
<option value="<?echo $id?>"><?echo $nome?></option>   
<?}?>
</select>

2º Select: Capiture the ID of the 1st Select and show the results belonging to them

<select  name="user_soli" style="width: 160px;" required >
<option value=""></option>   
<?      
$sql = "SELECT * FROM usuarios WHERE id_user = '$id_empresa'";
$resultado = mysql_query($sql) or die( mysql_error());
while ($linha = mysql_fetch_assoc($resultado)) {
$id = $linha["id_empresa"];
$nome = $linha["nome_fan"];
?>
<option value="<?echo $id?>"><?echo $nome?></option>   
<?}?>
</select>
  • You mean when you load the page one of the selects is filled in and you want the other to be filled in after the first one has been selected? you know how to use ajax?

  • Yes when selecting the company x logo already show all its users in the second dropdown.

  • you know what it is and how to use ajax?

  • No I know how to use ajax only PHP

  • Take a look here: http://answall.com/a/10766/129 meanwhile

  • It would be like two state selects one and the other blank. Qnd to choose the state in the other appear the cities

Show 1 more comment

1 answer

1

It seems complicated what you want, but it’s simpler than you think.

You will first have to separate the PHP file used to query the database.

Suppose you have a query file for users whose name is getUsuarios.php.

We will pass a value via parameter in javascript, to facilitate the example I will explain with the jQuery library.

Just mount a query via $get or $post, every time a "change" is performed in the main select, a query will be performed remotely and the value will return to the option of the second select.

Javascript (For example)

$('.PrimeiroSelectClass').on('change', function() {

   $.get( "getUsuarios.php", { id: this.value }).done(function( data ) {
     $( ".result" ).html( data );
     Console.log( "Resultado carregado" );
   });

});

PHP

Just take the value passed by parameter using $_GET or $_POST, perform the query with the code you have and return the values. In this example, the return will be in HTML

You can get more information on this link. http://www.tutorialspoint.com/jquery/ajax-jquery-get.htm

I recommend returning the values via JSON or XML. However via HTML is an agile solution for your case and functional.

I recommend you read it too:

Browser other questions tagged

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