Codeigniter Ajax dropdown -


controller(user.php)

<?php  class user extends ci_controller {      public function __construct() {         parent::__construct();         $this->load->model('country_model');     }      public function index() {         $this->load->view('register');     }      public function register() {         $data['countries'] = $this->country_model->get_countries();         $this->load->view('post_view', $data);     }      public function get_cities($country) {         $this->load->model('city_model');         header('content-type: application/x-json; charset=utf-8');         echo(json_encode($this->city_model->get_cities($country)));     }  } 

city_model

<?php  class city_model extends ci_model {      public function __construct() {         $this->load->database();     }      function get_cities($country = null) {         $this->db->select('id, city_name');          if ($country != null) {             $this->db->where('country_id', $country);         }          $query = $this->db->get('cities');         $cities = array();          if ($query->result()) {             foreach ($query->result() $city) {                 $cities[$city->id] = $city->city_name;             }             return $cities;         } else {             return false;         }     }  } ?> 

country_model

<?php  class country_model extends ci_model {      public function __construct() {         $this->load->database();     }      function get_countries() {         $this->db->select('id, country_name');         $query = $this->db->get('countries');         echo "countries";         $countries = array();          if ($query->result()) {             foreach ($query->result() $country) {                 $countries[$country->id] = $country->country_name;             }             return $countries;         } else {             return false;         }     }  } ?> 

view file

<html>      <head>         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>         <script type="text/javascript">// <![cdata[             $(document).ready(function() {                 $('#country').change(function() { //any select change on dropdown id country trigger code                     $("#cities > option").remove(); //first of clear select items                     var country_id = $('#country').val(); // here taking country id of selected one.                     $.ajax({                         type: "post",                         url: "http://localhost/task/user/get_cities/" + country_id, //here calling our user controller , get_cities method country_id                          success: function(cities) //we're calling response json array 'cities'                         {                             $.each(cities, function(id, city) //here we're doing foeach loop round each city id key , city value                             {                                 var opt = $('<option />'); // here we're creating new select option each city                                 opt.val(id);                                 opt.text(city);                                 $('#cities').append(opt); //here append these new select options dropdown id 'cities'                             });                         }                      });                  });             });             // ]]>         </script>     </head>     <body>         <?php $countries['#'] = 'please select'; ?>          <label for="country">country: </label><?php echo form_dropdown('country_id', $countries, '#', 'id="country"'); ?><br />         <?php $cities['#'] = 'please select'; ?>         <label for="city">city: </label><?php echo form_dropdown('city_id', $cities, '#', 'id="cities"'); ?><br />     </body> </html> 

i trying populate data database using codeigniter ajax dropdown. couldnt able value database. when select country name dropdown list city name should triggered out.i dont know did mistake. appreciatable.

try this:

 public function get_cities($country)             {                 $this->load->model('city_model');                  $result = $this->city_model->get_cities($country);                  $this->output->set_output(json_encode($result));             }      } 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -