I wanted to exercise with this library sqlsrv since with mysqli I worked without problem the exercise of the dependent select for regions, provinces and communes. My problem is the following I do not know how to debug with jquery in order to capture the error correctly but nevertheless I suppose that the data is sent and the one created in the other page appears where it is being rendered (getProvincia.php)
JQUERY
$(document).ready(function(){
$("#region").change(function () {
$("#region option:selected").each(function () {
id_region = $(this).val();
$.post("lib/getProvincia.php", { id_region: id_region }, function(data){
$("#provincia").html(data);
});
});
});
});
getProvincia.php
$query= "SELECT ProvCod, ProvDes, Id_Region FROM provincia WHERE
Id_Region = '$id_region' ORDER BY ProvDes";
$stmt = sqlsrv_query($con,$query);
$html= "<option value='0'>Seleccionar Provincia</option>";// esta opcion aparece como option al seleccionar cualquier region
while($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
$html.= "<option value='".$row['ProvCod']."'>".$row['ProvDes']."</option>";
}
echo $html;
?>
HTML
<form method="post" >
<div class="form-group">
<label class="col-sm-3 control-label" for="region">Región :</label>
<div class="col-sm-3">
<select class="form-control" id="region" name="region">
<option value="0">Seleccione</option>
<?php get_region();?> //este select si le llena correctamente
</select>
</div>
<label class="col-sm-3 control-label" for="provincia">Provincia :</label>
<div class="col-sm-3">
<select class="form-control" name="provincia" id="provincia">
</select>
</div>
</div>
</form>
function get_region(){
$con=conectar();
$query_reg = "SELECT id_Region, Descripcion FROM region";
if($stmt_reg = sqlsrv_query($con,$query_reg)){
while($row = sqlsrv_fetch_array($stmt_reg)){
?> <option value="<?php echo $row['id_region']; ?>" > <?php echo
$row['Descripcion']; ?> </option> <?php
}
}
}