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

1

Personal wave I have no idea how to proceed in this case

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

1st Select: List companies and get the selected Id

<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>

2nd Select: Capture the 1st Select ID 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>
    
asked by anonymous 13.05.2015 / 22:46

1 answer

1

It seems to be complicated what you want, but it is simpler than you think.

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

Suppose you have a file with the query of 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 made to the main select, a query will be performed remotely and the value will return to the second select option.

JavaScript (Example)

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

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

});

PHP

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

You can get more information at this link. link

I recommend that you return the values via JSON or XML. But via HTML is an agile solution for your case and functional.

I recommend you also read:

14.05.2015 / 20:59