I have these two selects:
<td>
<strong><label for="Valência">Valência</label></strong>
<select id="Valencia" name="Valencia" style="width:150px">
<option></option>
<?php
$sql = "SELECT descricaovalencia, Entidade FROM centrodb.utentes GROUP BY descricaovalencia, Entidade";
$qr = mysqli_query($conn, $sql);
while($ln = mysqli_fetch_assoc($qr)){
echo '<option value="'.$ln['descricaovalencia'].'"> '.$ln['descricaovalencia'].'</option>';
$valencia[$ln['descricaovalencia']]=array('Entidade'=>$ln['Entidade'],'Entidade'=>$ln['Entidade']);
}
?>
</select>
</td>
<td>
<strong><label for="Entidade">Entidade</label></strong>
<select id="Entidade" name="Entidade" style="width:150px">
<option></option>
<?php
foreach ($valencia as $key => $value) {
echo '<option data-id="'.$key.'" value="'.$value['Entidade'].'">'.$value['Entidade'].'</option>';
}
?>
</select>
</td>
I have this script:
$('#Valencia').change(function(){
var id = $('#Valencia option:selected').val();
$.each($('#Entidade option'),function(){
if($(this).attr('data-id')==id){
$(this).attr('selected',true);
}
});
});
ButherewhatIintendisthatonlyappearoncethenameofthevalencesandnotsorepeated.Resultofthesecondcombobox:
Here in this combobox is not returning all users.
But the idea was to select a value in the first combobox and in this all users who belong to that valence appear to select the desired one.