Good evening!
I want to populate the second select with the data depending on the first select, I have the following code
View
$(document).ready(function(){
$("#relatorio").change(function(){
$.ajax({
type: "POST",
url: "carregaDados",
data: {relatorio: $("#relatorio").val()},
dataType: "json",
success: function(json){
console.log(json);
var options = "";
$.each(json, function(key, value){
options += '<option value="' + key + '">' + value + '</option>';
});
$("#filtro").html(options);
}
});
});
});
<div class="form-group">
<label class="col-md-3 control-label">Relatório</label>
<div class="col-md-9">
<select class="form-control" name="relatorio" id="relatorio">
<option value="0">Escolha um relatório</option>
<option value="chamadosCliente">Chamados por cliente</option>
<option value="chamadosTecnico">Chamados por Tecnico</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="filtro">Filtro</label>
<div class="col-md-9">
<select class="form-control" name="filtro" id="filtro">
</select>
</div>
</div>
Controller
public function carregaDados(){
$this->load->model('clientes_model');
$clientes = $this->clientes_model->buscaNomeCliente();
echo json_encode($clientes);
}
Model
public function buscaNomeCliente(){
$this->db->select('id, razaoSocial');
$query = $this->db->get('clientes');
return $query->result();
}
But when it returns the pro ajax data in the View, it returns:
Array [ Object, Object ]
Do not return the data as it has to be, can anyone help me?
Thank you!