4
I need to use one select
dynamic on my page, in which when selecting the state(UF), it shows me in the next select
only the cities of this UF. Likewise the neighborhoods. All this information is already in the database.
I’m using Codeigniter, and I’ve tried several ways, but I couldn’t do it. Could someone give me a tutorial, or give me some instructions?
VIEW
<script type="text/javascript">
var site_url = '<?php echo site_url() ?>';
function search_cidades(uf){
var uf = uf;
$.post(site_url+"/clientes/search_cidades_uf", {
uf : uf
},function(data){
$('#cCidade').html(data);
});
}
</script>
<div class="col-lg-1">
<div class="form-group">
<label for="">UF</label>
<select style="text-transform:uppercase" name="cUf" class="form-control input cUf" id="cUf" onchange='search_cidades($(this).val())'>
<option>...</option>
<?php foreach($ufs_item as $uf):
if($uf['uf'] == $clientes_item['uf']){ ?>
<option value="<?= $uf['uf'] ?>" selected><?= $uf['uf']; ?></option>
<?php } else{ ?>
<option value="<?= $uf['uf'] ?>"><?= $uf['uf']; ?></option>
<?php } endforeach;?>
</select>
</div>
</div>
<div class="col-lg-3">
<div class="form-group">
<label for="">CIDADE</label>
<select style="text-transform:uppercase" name="cCidade" class="form-control input cCidade" id="cCidade">
</select>
</div>
</div>
CONTROLLER
class Clientes extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('clientes_model');
$this->load->model('cnaes_model');
$this->load->model('bairros_model');
}
public function listar_cliente($codigo) {
$data['clientes_item'] = $this->clientes_model->get_clientes($codigo);
$data['cnaes_item'] = $this->cnaes_model->get_cnaes();
$data['bairros_item'] = $this->bairros_model->get_bairros();
$data['ufs_item'] = $this->localizacao_model->get_ufs();
if (empty($data['clientes_item'])) {
show_404();
} else {
$data['codigo'] = $data['clientes_item']['codigo'];
$this->load->view('inc/header_view', $data);
$this->load->view('clientes/listar_cliente', $data);
}
}
public function search_cidades_uf(){
$this->load->model("localizacao_model");
$uf = $this->input->post("uf");
$cidades = $this->localizacao_model->get_cidades_uf($uf);
$option = "<option value=''></option>";
foreach($cidades -> result() as $linha) {
$option .= "<option value='$linha->codigo_cidade'>$linha->cidade</option>";
}
echo $option;
}
}
MODEL
class Localizacao_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function get_ufs() {
$this->db->order_by("uf", "asc");
$query = $this->db->get("tb_conf_ufs");
return $query->result_array();
}
public function get_cidades() {
$this->db->order_by("cidades", "asc");
$query = $this->db->get("tb_conf_cidades");
return $query->result_array();
}
public function get_cidades_uf($uf) {
$this->db->where("uf", $uf);
$this->db->order_by("cidades", "asc");
$query = $this->db->get("tb_conf_cidades");
return $query->result_array();
}
}
Shupupmagda, my question is: in the line of the code you suggested
$data['clientes_item'] = ['uf'=>'UF_DA_CIDADE_NO_DB'];//Faça seu $this->clientes_model->get_clientes($codigo) //retornar isso seguindo o padrão já visto acima
You ask to do in get_Clientes(Model) the same as you did get_Cidades(Model), but my get_clients have a lot of information, ie:
$data[] = [
'codigo' => $item['codigo'],
'data-cadastro' => $item['data-cadastro'],
'data-atualizacao' => $item['data-atualizacao'],
'razao-social' => $item['razao-social'],
'nome-fantasia' => $item['nome-fantasia'],
'cnpj' => $item['cnpj'],
'ie' => $item['nome-fantasia'],
'cnae' => $item['nome-fantasia'],
'situacao' => $item['nome-fantasia'],
'data-abertura' => $item['nome-fantasia'],
'data-baixa' => $item['nome-fantasia'],
'faturamento' => $item['nome-fantasia'],
'capital-social' => $item['nome-fantasia'],
'' => $item['nome-fantasia'],
'nome-fantasia' => $item['nome-fantasia'],
'nome-fantasia' => $item['nome-fantasia']
]
Do I really have to describe all fields in the database or does the result_array already do this? Because in your code you used the Result_array and then foreach.
Put it there like you tried to get people to guide you.
– Woss
Show us what you tried, otherwise we won’t believe you tried a lot of different ways. The community does not usually help those who did not try, but as you tried, just need to post your attempt. I look forward!
– user26552
How to select an option in one <select> and load related data in another? and Fill combobox with ajax
– rray
Asking in this way (without showing what you tried) is the same as asking me to do it for you. Show what you’ve done so people can help with the rest. Starting from scratch isn’t "helping".
– ShutUpMagda
Guys, I included the code I’m using... I did a test with the Uf field, and when selecting it shows through ALERT, which state I’m sending via post. As I said, the field of cities does not appear in any city, even marking the UF.
– crishenrique1986