I'm working with 2 select, the second depends on the data that is selected in the first one. I have not managed to make it work, so far the only thing it does is load the data corresponding to each select but separately.
**//Carga los datos de los departamentos en el Select//**
<div>
<select class="form-control" id="idDep" name="idDep" required>
<option value="">-- Escoja una opción --</option>
<?php foreach (departamentoData::getAll() as $departamento):?>
<option value="<?php echo $departamento->idDep; ?>"><?php echo $departamento->nombre; ?></option>
<?php endforeach; ?>
</select>
</div>
**//Carga los datos de los municipios en el Select//**
<div>
<select class="form-control" id="idMun" name="idMun" required>
<option value="">-- Escoja una opción --</option>
<?php foreach (municipioData::getAll() as $municipio):?>
<option value="<?php echo $municipio->idMun; ?>"><?php echo $municipio->nombre; ?></option>
<?php endforeach; ?>
</select>
</div>
//====================================================//
//Esta función es la que me permite cargar los departamentos
<?php
class departamentoData {
public static $tablename = "departamento";
public function departamentoData(){
}
public static function getAll(){
$sql = "select * from ".self::$tablename;
$query = Executor::doit($sql);
return Model::many($query[0],new departamentoData());
}
}
?>
//====================================================//
//Esta función es la que me permite cargar los municipios
<?php
class municipioData {
public static $tablename = "municipio";
public function municipioData(){
}
public static function getAll(){
$sql = "select * from ".self::$tablename;
$query = Executor::doit($sql);
return Model::many($query[0],new municipioData());
}
}
?>
//====================================================//
//Scrip para cargar los datos del select municipios
<script language="javascript">
$(document).ready(function(){
$("#idDep").change(function () {
$("#idDep option:selected").each(function () {
idDep = $(this).val();
$.post("municipioData.php", { idDep: idDep }, function(data){
$("#idMun").html(data);
});
});
})
});
</script>
The problem is that I load all the municipalities so select a department, I do not know what part is wrong, or if the code to load the municipalities is correct.
Can someone give me a little help?
In the municipalityData.php file, this is.
<?php
class municipioData {
public static $tablename = "municipio";
public function municipioData(){
}
public static function getAll(){
$sql = "select * from ".self::$tablename;
$query = Executor::doit($sql);
return Model::many($query[0],new municipioData());
}
}
?>
The following image is how I have the structure of the project.
Updating the question
I think the problem is in the file buscar.php because I do not know how it is executed or how the request is made to the database, should it include or require something? so that you can connect to the database and can you find the municipality of the selected department?
Code file search.php
<?php
if (isset($_POST["idDep"])){
/*Aquí si hace falta habrá que incluir la clase municipios con include*/
$intId=$_POST["idDep"];
/*Usamos un nuevo método que habremos creado en la clase municipio: getByDepartamento*/
$json=json_encode(municipioData::getByDepartamento($intId));
}else{
$json=json_encode(array('error'=>'No se recibió un valor de id departamento para filtar'));
}
print_r($json);
?>