Tatu Bogdan
Tatu Bogdan

Reputation: 596

Populate html form with database result array codeigniter

I have this form:

<form method = "POST" action = "<?php echo base_url('Usercontroller/insert') ?>">
  <div class="form-group">
    <label for="exampleInputEmail1">Apartament</label>
    <select name ="txtApartament1" class="form-control">
        <?php foreach($getEntry as $value) { ?>
        <option><?php echo $value->apartament ?></option>
        <?php }?>
    </select>
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Nume</label>
    <input type="text" name ="txtNume" class="form-control" id="exampleInputPassword1" placeholder="Nume">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Persoane</label>
    <input type="text" name ="txtPersoane" class="form-control" id="exampleInputPassword1" placeholder="Personae">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Mp</label>
    <input type="text" name ="txtMp" class="form-control" id="exampleInputPassword1" placeholder="Mp">
  </div>
  <div class="form-group">
    <label for="exampleInputPassword1">Comentariu</label>
    <input type="text" name ="txtComentariu" class="form-control" id="exampleInputPassword1" placeholder="Comentariu">
  </div>
  <button type="submit" class="btn btn-default">Salveaza</button>
</form>  

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 :

$(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');
    });
 });
});

And in my controller I return the array with data like this:

public function apartamentSelection() {
    $data= $this->input->post(null, true);
    $apartamentulAles=(array_keys($data)[0]);
    $query = $this->db->query("SELECT * FROM membri WHERE apartament = '".$apartamentulAles."' ");
    $result = $query->result_array();
    print_r($result);
}

And my response from the chosen option is looking something like this in the done function:

Done:  Array
(
[0] => Array
    (
        [id] => 5
        [apartament] => 5
        [per_id] => 1
        [nume] => Ion
        [persoane] => 4
        [mp] => 32
        [comentariu] => ddddd
    )

)

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?

Upvotes: 0

Views: 154

Answers (1)

Murtaza Bhurgri
Murtaza Bhurgri

Reputation: 398

You should send json response back to ajax call.

echo json_encode($result[0]);

Use dataType='JSON' attribute in $.ajax({}) call And then in your ajax done method get the response sent from php in responseData variable

.done(function(responseData) {
 $('#numeId').val(responseData.nume);
})

Upvotes: 2

Related Questions