I want to get data from database and show it to edit form. When I'm using json data, I've got problem on show the data to select option
field form. It still show option value id
ascending , not show value id selected
from database.
In the example, I need to change lesson
with id
number 7
. And in the select option
, it shows lesson from id
number 1
, but when I click the select option
, it shows id
with number 7
.
Here's the jquery code
function edit(id){
$.ajax({
url : url + 'Lesson/find_by_id',
type: 'POST',
data: {id: id},
success: function(response){
var jsonData = JSON.parse(response);
$('input[name="id"]').val(jsonData.id);
$('select[name="lesson"]').val(jsonData.lesson_id);
$('textarea[name="description"]').val(jsonData.description);
}
});
$('#editModal').modal('show');
}
Here's the form code
<select class="form-control select-search" name="lesson" style="width: 100%" required="">
<?php foreach($lesson as $row): ?>
<option value="<?=$row->lesson_id?>" > <?=strtoupper($row->name)?> </option>
<?php endforeach; ?>
</select>
Here's the controller code
public function find_by_id(){
$lesson_id = $this->input->post('id');
$data = $this->lesson->find_lesson($lesson_id);
echo json_encode($data);
}
Do you know how to fix the code ?
Thank you