I have a select that depends on another, when selecting the first one it throws me an error in a modal window of the browser "parsererror" and it does not load the data associated with the option of the first select. I have looked for the way to solve the error and nothing. Someone can help me, here I leave the code.
//Tablas involucradas en el select dependiente
create table departamento (
idDep int(10) not null,
nombre varchar(25) not null);
create table municipio (
idMun int(10) not null,
nombre varchar(25) not null,
idDep int,
FOREIGN KEY (idDep) REFERENCES departamento(idDep));
//Cargo los select
<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>
<select class="form-control" id="idMun" name="idMun" required>
<option value="">-- Escoja una opción --</option> </select>
Then the classes departmentData.php and municipalityData.php
//departamentoData.php
<?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());
}
public static function getByDepartamento($idDep){
$sql = "select * from ".self::$tablename." WHERE idDep=".$intId;
$query = Executor::doit($sql);
return Model::many($query[0],new departamentoData()); }
}
?>
//municipioData.php
<?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());
}
public static function getByDepartamento($intId){
$sql = "select idMun, nombre from ".self::$tablename. " where idDep= \"$intId\"";
$query = Executor::doit($sql);
return Model::many($query[0],new municipioData());
}
}
?>
And finally the file search.php and javascript
//buscar.php
<?php
if (isset($_POST["idDep"])){
include class ("municipioData.php)";
$intId=$_POST["idDep"];
$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);
?>
//código Javascript esta en un archivo llamado layout.php
<script language="javascript">
$(function() {
$( "#idDep" ).on( "change", function() {
var intId=$(this).val();
var datos = {idDep: intId };
var url='http://localhost/escuela/core/app/layouts/buscar.php';
var request = $.ajax
({
url: url,
method: 'POST',
data: datos,
dataType: 'json'
});
request.done(function( respuesta )
{ console.log(respuesta);
if(!respuesta.hasOwnProperty('error')){
$.each(respuesta, function(k, v) {
$('#idMun').append('<option value="' + k.idMun + '">' + v.nombre + '</option>');
});
}else{
}
});
request.fail(function( jqXHR, textStatus )
{
alert( "Hubo un error: " + textStatus );
});
});
});
</script>