I have two selections, one I charge the countries, in the other the provinces that "reacts" according to the country that was selected first. Just consulted how to bring from a BD the code of a country and make that select countries show me the country that comes from the BD, but I also have a province that I bring from the BD. Now, applying that load, the "select" of provinces "does not react" since it is not loaded according to the selected select by means of the data of the BD. The country selection is loaded with:
<select id="pais" class="ui fluid search dropdown" name="pais">
<option value="0"></option>
<?php
$conexion = new Conexion();
$stmt = $conexion -> prepare("SELECT paiscod, paisnom FROM paises ORDER BY paisnom");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ( $row['paiscod'] == $pais ) { ?>
<option value = "<?php echo $row['paiscod']?>" selected><?php echo ($row['paisnom'])?></option>
<?php } else { ?>
<option value = "<?php echo $row['paiscod']?>"><?php echo ($row['paisnom'])?></option>
<?php }
}
?>
</select>
<select id="provincias" class="ui fluid search dropdown" name="selectProvincias">
<option value=""></option>
</select>
$id_pais = $_POST['id_pais'];
$conexion = new Conexion();
$stmt = $conexion -> prepare("SELECT provincod, provinnom FROM provincia WHERE paiscod = :valor");
$stmt->bindParam(':valor', $id_pais);
$stmt->execute();
if ($stmt->rowCount() > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$html .= '<option value = "'.$row['provincod'].'">'. $row['provinnom'].'</option>';
}
}
echo $html;
<script language="javascript">
$(document).ready(function() {
$("#pais").change(function() {
$("#pais option:selected").each(function() {
id_pais = $(this).val();
$.post("provincias.php", {
id_pais: id_pais
}, function(data) {
$("#provincias").html(data);
});
});
})
});
</script>
QUESTION 1: Why does not the js recognize the change in the country select when it takes the value from the BD?
QUESTION 2: How I pass the code of the province that comes by BD to show me the selected province?