title: Populate html form with database result array codeigniter
body: <p>I have this form:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form method = "POST" action = "&lt;?php echo base_url('Usercontroller/insert') ?&gt;"&gt;
    &lt;div class="form-group"&gt;
      &lt;label for="exampleInputEmail1"&gt;Apartament&lt;/label&gt;
      &lt;select name ="txtApartament1" class="form-control"&gt;
          &lt;?php foreach($getEntry as $value) { ?&gt;
          &lt;option&gt;&lt;?php echo $value-&gt;apartament ?&gt;&lt;/option&gt;
          &lt;?php }?&gt;
      &lt;/select&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label for="exampleInputPassword1"&gt;Nume&lt;/label&gt;
      &lt;input type="text" name ="txtNume" class="form-control" id="exampleInputPassword1" placeholder="Nume"&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label for="exampleInputPassword1"&gt;Persoane&lt;/label&gt;
      &lt;input type="text" name ="txtPersoane" class="form-control" id="exampleInputPassword1" placeholder="Personae"&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label for="exampleInputPassword1"&gt;Mp&lt;/label&gt;
      &lt;input type="text" name ="txtMp" class="form-control" id="exampleInputPassword1" placeholder="Mp"&gt;
    &lt;/div&gt;
    &lt;div class="form-group"&gt;
      &lt;label for="exampleInputPassword1"&gt;Comentariu&lt;/label&gt;
      &lt;input type="text" name ="txtComentariu" class="form-control" id="exampleInputPassword1" placeholder="Comentariu"&gt;
    &lt;/div&gt;
    &lt;button type="submit" class="btn btn-default"&gt;Salveaza&lt;/button&gt;
  &lt;/form&gt;  </code></pre>
  </div>
  </div>
  </p>
  
  <p>And based on the chosen option from the drop down in the Apartament field I want to populate the other field with values that I extract from database, in order to obtain this I made a ajax that will send the option chosen, like this :</p>
  
  <pre><code>$(document).ready(function(){
  $( ".form-control" ).change(function() {
      var apartament = $(this).val();
      console.log(apartament);
      $.ajax({ 
          url: 'Usercontroller/apartamentSelection',
          data: apartament,
          type: 'post'
      }).done(function(responseData) {
          console.log('Done: ', responseData);
      }).fail(function() {
          console.log('Failed');
      });
   });
  });
  </code></pre>
  
  <p>And in my controller I return the array with data like this:</p>
  
  <pre><code>public function apartamentSelection() {
      $data= $this-&gt;input-&gt;post(null, true);
      $apartamentulAles=(array_keys($data)[0]);
      $query = $this-&gt;db-&gt;query("SELECT * FROM membri WHERE apartament = '".$apartamentulAles."' ");
      $result = $query-&gt;result_array();
      print_r($result);
  }
  </code></pre>
  
  <p>And my response from the chosen option is looking something like this in the done function:</p>
  
  <pre><code>Done:  Array
  (
  [0] =&gt; Array
      (
          [id] =&gt; 5
          [apartament] =&gt; 5
          [per_id] =&gt; 1
          [nume] =&gt; Ion
          [persoane] =&gt; 4
          [mp] =&gt; 32
          [comentariu] =&gt; ddddd
      )
  
  )
  </code></pre>
  
  <p>How I can take the values from my response array and populate the above form, in the Nume field I should get Ion, in Persoane 4 and so on?</p>