title: How to validate dependent drop down using codeigniter form validation library?
body: <p>I want to try dependent drop down validation using CodeIgniter form validation library. Here is some problem when I select the state then result in its show on district drop down but here is a problem If I Press the submit button (after the page reloads) then the state hold their value but the district cannot hold district's value. How to solve this problem. Thanks in advance.</p>
  
  <hr>
  
  <h2><strong>Controller</strong></h2>
  
  <pre><code>&lt;?php
  class Dependent extends CI_Controller
  {
      public function index()
      {
          $this-&gt;load-&gt;library("form_validation");
  
          if(isset($_POST["submit"]) == "Submit")
          {
              $this-&gt;form_validation-&gt;set_rules("state","State","required");
              $this-&gt;form_validation-&gt;set_rules("district","District","required");
              $this-&gt;form_validation-&gt;set_rules("name","Name","required");
              if($this-&gt;form_validation-&gt;run())
              {
                  //success
              }
          }
  
          $this-&gt;load-&gt;model("Dependents");
          $data["state"] = $this-&gt;Dependents-&gt;get_state();
          $this-&gt;load-&gt;view("drop",$data);
      }
      public function district()
      {
          $this-&gt;load-&gt;library("form_validation");
          $this-&gt;load-&gt;model("Dependents");
  
          $id = $_POST["id"];
          $district = $this-&gt;Dependents-&gt;get_district($id);
          $output = "";
          $output .= "&lt;option value=''&gt;Select District&lt;/option&gt;";
          foreach($district as $row)
          {
              $output .= "&lt;option value='".$row["id"]."'&gt;".$row["district"]."&lt;/option&gt;";
          }
          echo $output;
      }
  }
  ?&gt;
  </code></pre>
  
  <h2><strong>Model</strong></h2>
  
  <pre><code>&lt;?php
  class Dependents extends CI_Model
  {
      public function get_state()
      {
          return $this-&gt;db-&gt;get("tbl_state")-&gt;result_array();
      }
      public function get_district($id)
      {
          return $this-&gt;db-&gt;where("state",$id)-&gt;get("tbl_district")-&gt;result_array();
      }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;?php
      echo validation_errors();
  ?&gt;
  
  &lt;form method="post" action=""&gt;
      &lt;div class="form-group"&gt;
          &lt;select class="form-control" id="state" name="state"&gt;
              &lt;option value=""&gt;Select State&lt;/option&gt;
              &lt;?php
                  foreach ($state as $row) {
                  ?&gt;
              &lt;option value="&lt;?php echo $row["id"]; ?&gt;"&lt;?php echo set_select("state", $row["id"]); ?&gt;&gt;&lt;?php echo $row["state"]; ?&gt;&lt;/option&gt;
              &lt;?php
                  }
              ?&gt;
          &lt;/select&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;select class="form-control" id="district" name=district&gt;
              &lt;option value=""&gt;Select District&lt;/option&gt;
          &lt;/select&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;input type="text" name="name" placeholder="Enter Name" class="form-control"&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;input type="submit" name="submit" value="Submit" class="btn btn-success btn-sm"&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  &lt;script type="text/javascript"&gt;
      $(function(){
      $("#state").change(function(){
          var id = $(this).val();
  
          $.ajax({
              url: "&lt;?php echo base_url() . "Dependent/district"; ?&gt;",
              type: "POST",
              data:{id: id},
              success: function(data)
              {
              $("#district").html(data);
              }
          });
      });
      });
  &lt;/script&gt;
  </code></pre>