Member Avatar for anmol.raghuvanshi1

I am trying to populate dropdown with database what i am actually trying to do i have 3 drop downs country ,state and city and i want if user select any country crossponding states are shown in dropdown and crossponding cities releated to state are shown in city dropdown.I don't how to achieve this all fields will be saved in tabl_usrs and and i have nm_country,nm_state and nm_city table from where country states and cities are shown in tabl_usrs i have country,state and city field wwhich act as foriegn key??
any links will be welcome.Hope any one can help.below is just i am trying to do this seprate from project

Controller

$data['city_list'] = $this->City_model->get_dropdown_list();
$this->load->view('my_view_file', $data); 
Model

function get_dropdown_list()
{
$this->db->from('city');
$this->db->order_by('name');
$result = $this->db->get();
$return = array();
if($result->num_rows() > 0) {
foreach($result->result_array() as $row) {
$return[$row['id']] = $row['name'];
}
}

        return $return;

}
View

<?php echo form_dropdown('city_id', $city_list, set_value('city_id', $city_id));

Recommended Answers

All 2 Replies

You have 24 lines of code (along with empty lines) and I could make more than 30 fast comments of why this MVC implementation has serious problems.

But lets stay in your question , do you know how to perform an AJAX request?

Do you know how dynamically change the options of a select client side?

Do you know how to make that select visible or not visible client side?

Member Avatar for anmol.raghuvanshi1
//my view performing some ajax request
<script>
                        $(document).ready(function() {  
                     $("#country").change(function(){  
                     /*dropdown post *///  
                     $.ajax({  
                        url:"<?php echo  
                        base_url();?>Country_state/buildDropStates",  
                        data: {id:  
                           $(this).val()},  
                        type: "POST",  
                        success:function(data){  
                        $("#state").html(data);  
                     }  
                  });  
               });  
            });   
            </script>
                    <label for="country">Country</label>
                         <?php echo form_dropdown('country',$country,'','class="required" id="country" '); ?>
                         <br />
                         <br />


                        <label for="state">State</label>
                         <select name="state" id="state">  
                             <option value="">Select</option>  
                          </select>
                        <label for="city">City</label>
                         <select>
                          <option>Select City</option>
                          <option value="volvo">Volvo</option>
                          <option value="saab">Saab</option>
                          <option value="mercedes">Mercedes</option>
                          <option value="audi">Audi</option>
                        </select> 

my controller

//my controller 
<?php  
       class Country_state extends CI_Controller {  
       public function __construct()  
       {  
          parent::__construct();  
          $this->load->database();  
          $this->load->helper('url');  
          $this->load->helper('form');  
          $this->load->model('Country_states_cities');  
       }  
       public function index()  
       {  
          //starts by running the query for the countries dropdown  
          $data['countryDrop'] = $this->Country_states_cities->getCountries();  
          //loads up the view with the query results  
          $this->load->view('camping_registration', $data);  
       }  
       //call to fill the second dropdown with the states 
       public function buildDropStates()  
       {  

          //set selected country id from POST  
          echo $id_country = $this->input->post('id');  
          //var_dump($id_country);
          //run the query for the cities we specified earlier  
          $districtData['districtDrop']=$this->Country_states_cities->getCityByCountry($id_country);  
          $output = null;  
          foreach ($districtData['districtDrop'] as $row)  
          {  
             //here we build a dropdown item line for each  query result  
             $output .= "<option value='".$row->sub_name."'>".$row->sub_name."</option>";  
          }  
          echo $output;  
         // var_dump($output);
       }  
    }   

my model

<?php  
    class Country_states_cities extends CI_Model {  
       public function __construct()  
       {  
          $this->load->database();  
       }  
       //fill your contry dropdown  
       public function getCountries()  
       {  
          $this->db->select('country_id,country_name');  
          $this->db->from('nm_country');  
          $query = $this->db->get(); 
          // the query mean select country_id,country_name from  nm_country
          foreach($query->result_array() as $row){  
             $data[$row['country_id']]=$row['country_name'];  
          }  
          // the fetching data from database is return  
          return $data;

       }  
       //fill your state dropdown depending on the selected country  
       public function getCityByCountry($country_id)  
       {  
          //$this->db->select('state_id,state_name');  
         // $this->db->from('nm_state');  
         // $this->db->where('country_id',$country_id);  
          $query =  $this->db->query("SELECT state_id,state_name FROM `nm_state` WHERE country_id='$country_id'");
          //var_dump($query);
          $result = $query->result_array();  
          print_r($result);
          return $result;  
       }  
    }   

    ?>

after more learning and doing i have above code but it's problem is returning array is empty i can't able to figure out why??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.