How can I display the data using AJAX? After making the data call to the controller, the JSON data is available but the data for "name", "address", and "telp" based on the "id_data" is not showing up. How can I display this data?
Views
<input id="id_data" value="5" />
<input id="name">
<input id="address">
<input id="telp">
<script type="text/javascript">
$(document).ready(function() {
var id = $('#id_data').val();
$.ajax({
url : "<?php echo site_url('my_controllers/show_data/')?>" + id,
type: "GET",
dataType: "JSON",
success: function(data)
{
$('#name').text(data.name);
$('#address').text(data.address);
$('#telp').text(data.telp);
}
});
})
</script>
Controllers
function show_data($id) {
$data = $this->my_models->get_data($id);
echo json_encode($data);
}
Models
function get_data($id) {
$query = $this->db->select('*')
->from('tb_student')
->where('id', $id)
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
Result JSON
[
{"id":"5","name":"John","address":"Miami","telp":"012345678"},
{"id":"5","name":"Smith","address":"Chichago","telp":"012345678"},
{"id":"5","name":"Steve","address":"Texas","telp":"012345678"},
]