title: onchange using ajax, codeigniter, dropdown
body: <p>I am facing a problem with onchange event in codeigniter...I am showing a country state onchange event but the value of the country is taken zero due to which it is not changing the states accordingly.</p>
  
  <p>Controller:</p>
  
  <pre><code>    &lt;?php
      class countries extends CI_Controller {
     public function __construct()
     { 
              parent::__construct();
      $this-&gt;load-&gt;database();
      $this-&gt;load-&gt;helper('form');
      $this-&gt;load-&gt;helper('url');
      $this-&gt;load-&gt;model('countries_model');
  
      $this -&gt; load -&gt; model('country_model');
      $this -&gt; load -&gt; model('state_model');
  
  }
  
  public function index()
  {
  $this-&gt;data['countries'] = $this -&gt; country_model -&gt; get_countries();
    $view_data = array();
    $this-&gt;display_data();
  $this-&gt;load-&gt;view('view', $this-&gt;data);
  }
  
  
      public function get_states(){
          log_message('debug', 'Inside uController :get_states');
          log_message('debug', 'Value of country is '+$this-&gt;input-&gt;post('country_id'));
          log_message('debug', 'Value of country is '+$this-&gt;input-&gt;post('data'));
          var_dump(data);
          $country=$this-&gt;input-&gt;post('country');
          //echo $country;
  
     $this-&gt;load-&gt;model('state_model');
    header('Content-Type: application/x-json; charset=utf-8');
    echo(json_encode($this-&gt;state_model-&gt;get_states($country)));
      }
  }
  
  
  public function dataupdate()
  {
        $i=$this-&gt;input-&gt;post('country_id');
      $s=$this-&gt;input-&gt;post('state_id');
          $this-&gt;model-&gt;UpdateData();
          $this-&gt;load-&gt;view('success');
      }
  
  ?&gt;
  </code></pre>
  
  <p>Model:</p>
  
  <pre><code>    &lt;?php
     class Countries_model extends CI_Model {
     public function __construct()
  {
  
      parent::__construct();
  
  }
      public function get_countries()
     {
  
      log_message('debug', ' poc_dropdown ######################################################inside model ');
      $query ="SELECT * FROM MCountry";
  
      $query_result = $this-&gt;db-&gt;query($query);
      log_message('debug', ' poc_dropdown ######################################################calling db query ');
      $data = array();
      foreach($query_result-&gt;result() as $row){
          $data[] = $row;
      }
      return $data;
  }
  }
  
       function UpdateData($dt)
  {
  $id=$this-&gt;input-&gt;post('pk');
  
   $data=array(
        'Col_Country'=&gt;$this-&gt;input-&gt;post('country_id'),
      'Col_State'=&gt;$this-&gt;input-&gt;post('state_id'),
         );
    $this-&gt;db-&gt;where('pk',$id);
    $this-&gt;db-&gt;update('User',$data);
       }
  ?&gt;
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>    &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;
  
              &lt;script type="text/javascript"&gt;// &lt;![CDATA[
  
              $(document).ready(function(){      
                  $('#country').change(function(){ //any select change on the dropdown with id country trigger this code        
                    alert("inside country");
                    // $("#states &gt; option").remove();
                      $("#states &gt; option").remove();
                      alert("inside country2");
                      //$("#cities &gt; option").remove();   //first of all clear select items.
                  alert($('#country').val());
                      var country_id = {
                              //alert("inside country3");
                              //country:$('#country').val()
                              country:$('#country option:selected').val()  
                              //alert("inside country4");
                      };  // here we are taking country id of the selected one.
  
                      alert(country_id);
                      $.ajax({
                          type: "POST",
                          url: "&lt;?php echo site_url('u/get_states'); ?&gt;", //here we are calling our user controller and get_cities method with the country_id
                          data:country_id,
                          datatype : "json",
                          success: function(states) //we're calling the response json array 'states'
                          {
  
                              $.each(states,function(id,state) //here we're doing a foeach loop round each city with id as the key and city as the value
                              {
                                 var opt = $('&lt;option /&gt;'); // here we're creating a new select option with for each city
                                 opt.val(id);
                                  opt.text(state);
                                  $('#states').append(opt); //here we will append these new select options to a dropdown with the id 'states'
                              });
                          }
  
                      });
  
                  });
  
              });
      &lt;/script&gt;
  
      &lt;tr&gt;&lt;th&gt;&lt;label for="country"&gt;Country: &lt;/label&gt;&lt;/th&gt;&lt;td&gt;
               &lt;?php $countries['#'] = 'Please Select'; ?&gt;
  
                  &lt;?php echo form_dropdown('country_id', $countries, '#', 'id="country"','name="country"'); ?&gt;
                  &lt;/td&gt;&lt;/tr&gt;
  
      &lt;tr&gt;&lt;th&gt; &lt;label for="state"&gt;State: &lt;/label&gt; &lt;/th&gt;&lt;td&gt;
                   &lt;?php $states['#'] = 'Please Select'; ?&gt;
                  &lt;?php echo form_dropdown('state_id', $states, '#', 'id="states"','name="states"'); ?&gt;
                  &lt;/td&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>State_model:</p>
  
  <pre><code>    &lt;?php
     class state_model extends  CI_Model {
  
      public function __construct() {
  
              $this -&gt; load -&gt; database();
  
      }
  
      function get_states($country){//
  
  
    $this-&gt;db-&gt;select('pkMState, State');
  
   if($country != NULL){
        $this-&gt;db-&gt;where('fkMCountry',$country); //$country
    }
  
    $query = $this-&gt;db-&gt;get('MState');
   // $d=$query-&gt;result();
    $states = array();
  
    if($query-&gt;result()){
        foreach ( $query-&gt;result() as $state) {  //$query-&gt;result()
  
           $states[$state -&gt; pkMState] = $state -&gt; State;
        }
    return   $states;
    }
    else{
       return 0;
    }
  
      }
      }
  
     ?&gt;
  </code></pre>
  
  <p>Country_model</p>
  
  <pre><code>public function get_countries() {
  
              $this -&gt; db -&gt; select('pkCountry, CountryName');
              $query = $this -&gt; db -&gt; get('MCountry');
  
              $countries = array();
  
              if ($query -&gt; result()) {
                      foreach ($query-&gt;result() as $country) {
                              $countries[$country -&gt; pkCountry] = $country -&gt; CountryName;
                      }
                      return $countries;
              } else {
                      return 0;
              }
      }
  </code></pre>