Good morning
I have a problem with the combobox
and the jquery , the event is not "activated" from the drop-down menu (I have 2 dependent menus mark-> model)
script that is in the html head
<script language="javascript" src="js/jquery-3.2.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("cbx_marca").change(function () {
id_marca = $(this).val()
$.ajax({
type:"POST",
url:"includes/getMarca.php",
data:{ id_marca: id_marca },
success: function(data){
$("cbx_modelo").html(data);
}
})
})
});
</script>
HTML
<form id="combo" name="combo" method="POST" action="guardar.php">
<div>Seleccione la Marca:
<select id="cbx_marca" name="cbx_marca">
<option value="0">Seleccionar Marca</option>
<?php WHILE ($row=$resultado->fetch_assoc()){ ?>
<option value="<?php echo $row["IdMarca"];?>"><?php echo
$row["NombreMarca"];?></option>
<?php } ?>
</select>
</div>
<div>Seleccione el Modelo:
<select id="cbx_modelo" name="cbx_modelo">
</select>
</div>
<input type="submit" id="enviar" name="enviar" value="Guardar" />
</form>
The getMarca file
require ('../conexion.php');
if ( !empty($_POST['id_marca'])) {
$id_marca = (int)$_POST['id_marca'];
}
else{
echo 'NO se recibieron datos POST';
$id_marca = 2;
}
$query2 = "SELECT IdModelo, NombreModelo FROM modelo WHERE IdMarca =
'$id_marca' ORDER BY NombreModelo";
$resultado2 = $mysqli->query($query2);
$html= "<option value='0'>Seleccionar Modelo</option>";
while($row2 = $resultado2->fetch_assoc())
{
$html.= "<option value='".$row2['IdModelo']."'>".$row2['NombreModelo']."</option>";
}
echo $html;
With the if
of getMarca, I checked that the post does not arrive and I would not know if it is because the events are not "activated" or I'm doing something wrong.
Excuse my ignorance I am still studying and I hope you can help me.
PS: It's the first time I use this page, if I'm missing something or anything, tell me.
Last edition and functional
<script language="javascript">
$(document).ready(function(){
$("#cbx_marca").change(function () {
$("#cbx_marca option:selected").each(function () {
id_marca = $(this).val();
$.ajax({
type:"POST",
url:"includes/getMarca.php",
data:{ id_marca: id_marca },
success: function(data){
$("#cbx_modelo").html(data);
}
})
});
})
});