I have two select, one I load the countries, in the other the provinces that "reacts" according to the country that was selected first. I recently consulted how to bring a country code from a DB and make that country select show me the country that comes from the DB, but I also have a province that I bring from the DB. Now, applying this load, the select of provinces "does not react" since it is not loaded according to the select selected by means of the database data. The select of countries 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 doesn't the js recognize the change in the select country when it takes the value from the DB?
QUESTION 2: How do I pass the code of the province that comes through BD so that it shows me the selected province?
I will allow myself to comment on a few things about your code, in parts.
I wanted to tell you in comment, but the space would not give me.
You don't say it in your question, but I assume this code is in a different file than
provincias.php
Let's say the file is called
paises.php
.Code in
paises.php
:1. Do not prepare something already prepared
Prepared queries are used when data from elsewhere is involved. Not the case here. The query is already prepared , there is no risk of sending it directly through the method
query
:Therefore, this would suffice:
2. The whole procedure
while
you have can be simplified like this:3. The script must be in
paises.php
:You can improve it, as has been said in comments. I imagine that in the HTML you have a select whose id is
provincias
.The script, while it may work, is using deprecated code:
$(document).ready(function() {...
it is deprecated as of jQuery3. It is recommended to use$(function() {...
.Similarly, Ajax requests are recommended to have at least the ability to handle
done
on success, andfail
on failure ( see here ).The request made through Ajax/jQuery must be directed to a different file. In this case it is the file indicated in the url of the request
provincias.php
.Code in
provincias.php
: