0

Como hace, este archivo es cargar la librería de jquery para poder utilizar ajax y el código jquery donde diremos que cuando el select con id country cambie de opción, en el evento change

table pp_countries
------------------
id
country_name

table pp_cities
---------------
id
country_ID
city_name

El controlador home.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->ads = '';
        $this->ads = $this->ads_model->get_ads();
    }

    public function index()
    {

        //Comuna
        $data['cities_res'] = $this->cities_model->get_all_cities();

        //Cuidad
        $data['country_res'] = $this->countries_model->get_all_countries();

        $this->load->view('home_view',$data);
    }
}

el modelo cities_model.php

public function get_all_cities($id) {
        $this->db->select('*');
        $this->db->from('pp_cities');
        $this->db->order_by("sort_order", "ASC");
        $this->db->order_by("city_name", "ASC");
        $Q = $this->db->get();
        if ($Q->num_rows > 0) {
            $return = $Q->result();
        } else {
            $return = 0;
        }
        $Q->free_result();
        return $return;
    }

el modelo countries_model.php

public function get_all_countries() {
        $this->db->select('*');
        $this->db->from('pp_countries');
        $this->db->order_by("id", "ASC");
        $Q = $this->db->get();
        if ($Q->num_rows > 0) {
            $return = $Q->result();
        } else {
            $return = 0;
        }
        $Q->free_result();
        return $return;
    }

la vista home_search.php

<?php echo form_open_multipart('buscador/search',array('name' => 'jsearch', 'id' => 'jsearch'));?>
    <div class="col-md-4">
          <!--<input type="text" required name="job_params" id="job_params" class="form-control" placeholder="Job title or Skill" />-->

      <select class="form-control" name="jcountry" id="jcountry">

        <option value="" selected>Select City</option>
        <?php if($country_res): foreach($country_res as $country):?>
          <option value="<?php echo $country->country_name;?>"><?php echo $country->country_name;?></option>
        <?php endforeach; endif;?>
      </select>
    </div>

    <div class="col-md-4">
      <select class="form-control" name="jcity" id="jcity">

        <option value="" selected>Select City</option>
        <?php if($cities_res): foreach($cities_res as $cities):?>
            <option value="<?php echo $cities->city_name;?>"><?php echo $cities->city_name;?></option>
        <?php endforeach; endif;?>
      </select>
    </div>

    <div class="col-md-2">
      <input type="submit" name="job_submit" class="btn" id="job_submit" value="Search"  />
    </div>
<?php echo form_close();?> 

1 respuesta 1

1

En base a un proyecto que yo tengo de esa misma manera tu código quedaía de la siguiente manera:

Controlador:

class Home extends CI_Controller {
public function __construct(){
    parent::__construct();
    $this->ads = '';
    $this->ads = $this->ads_model->get_ads();
}

public function index()
{
    //Cuidad
    $data['country_res'] = $this->countries_model->get_all_countries();

    $this->load->view('home_view',$data);
}

public function get_cities(){

        $options = "";
        if ($this->input->post('id_country')) {
            $id_country = $this->input->post('id_country');
            $cities_res = $this->cities_model->get_all_cities($id_country);
            foreach ($cities_res as $fila) {
                ?>
                <option value="<?php echo $fila->id ?>"><?php echo $city_name ?></option>
                <?php
            }
        }
}

}

Cities_model:

public function get_all_cities($id) {
        $this->db->select('*');
        $this->db->from('pp_cities');
        $this->db->where('country_ID', $id);
        $this->db->order_by("sort_order", "ASC");
        $this->db->order_by("city_name", "ASC");
        $Q = $this->db->get();
        if ($Q->num_rows > 0) {
            $return = $Q->result();
        } else {
            $return = 0;
        }
        $Q->free_result();
        return $return;
    }

View:

<?php echo form_open_multipart('buscador/search',array('name' => 'jsearch', 'id' => 'jsearch'));?>
    <div class="col-md-4">
          <!--<input type="text" required name="job_params" id="job_params" class="form-control" placeholder="Job title or Skill" />-->

      <select class="form-control" name="jcountry" id="jcountry">

        <option value="" selected>Select City</option>
        <?php if($country_res): foreach($country_res as $country):?>
          <option value="<?php echo $country->id;?>"><?php echo $country->country_name;?></option>
        <?php endforeach; endif;?>
      </select>
    </div>

    <div class="col-md-4">
      <select class="form-control" name="jcity" id="jcity">

          <option value=""></option>
      </select>
    </div>

    <div class="col-md-2">
      <input type="submit" name="job_submit" class="btn" id="job_submit" value="Search"  />
    </div>
<?php echo form_close();?> 

Via Ajax te traes los resultados desde tu controlador.

Ajax:

$(document).ready(function(){
                $("#jcountry").change(function () {
                    $("#jcountry option:selected").each(function () {
                        id_country = $('#jcountry').val();
                        $.post("<?php echo base_url() ?>home/get_cities", {
                            id_country: id_country
                        }, function (data) {
                            $("#jcity").html(data);
                        });
                    });
                });
            });

Para mayor información te podrias apoya del siguiente tutorial:

https://www.uno-de-piera.com/cargar-select-dependiente-con-ajax-y-codeigniter/

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.