I'm doing a php project where I have to register a student in a section of a course, I have a first combobox where I show the courses that are available and then a second combobox that depending on the course you select, shows the sections that are available available for that course. the first combobox I have it this way
<?php $query = "SELECT id_curso, nombre_curso, estatus FROM curso WHERE estatus='disponible' ";
$resultado=$mysqli->query($query); ?>
<div class="form-group">
<label style="margin-top: 6px; margin-left: 25px" for="cbx_curso" class="col-md-2 control-label">Taller o Curso:<span class="asterisco">*</span></label>
<div class="col-md-3 " style="display: inline-block; width: 200px; margin-left: -80px; ">
<select name="cbx_curso" id="cbx_curso" class="form-control">
<option value="0" style="">Seleccionar Curso</option>
<?php while($row = $resultado->fetch_assoc()) { ?>
<option value="<?php echo $row['id_curso']; ?>"><?php echo $row['nombre_curso']; ?></option>
<?php } ?>
</select>
</div>
</div>
the second dependent combobox comes from here
$id_curso = $_POST['id_curso'];
$queryS = "SELECT id_seccion, descripcion FROM seccion WHERE curso_id_curso = '$id_curso'";
$resultadoS = $mysqli->query($queryS);
$html= "<option value='0'>Elija una opción</option>";
while($rowS = $resultadoS->fetch_assoc())
{
$html.= "<option value='".$rowS['id_seccion']."'>".$rowS['descripcion']."</option>";
}
echo $html;
?>
I make it work with this script
<script type="text/javascript">
$(document).ready(function(){
$("#cbx_curso").change(function () {
$("#cbx_curso option:selected").each(function () {
id_curso = $(this).val();
$.post("../includes/getSeccion.php", { id_curso: id_curso }, function(data){
$("#cbx_seccion").html(data);
});
});
})
});
</script>
but it is happening to me that when I go to register and select the first course, I take the second course. If I select the second one, I take the third one and so on. In the case when I have a course with more than two sections, for example four sections and I select the third section, the second section of that combobox is always selected, so select the fourth section or if there is a fifth, it is always saved in the database It is the second section. who can help me I would appreciate it