I'm new to javascript and I tried to follow this tutorial.
I have a textbox(input) and I want to use the jQuery's autocomplete by loading some data from MySQL.
This is my controller code :
public function autocomplete() {
if($this->input->post('txt_nama'))
$this->absensi_m->get_umat($this->input->post('txt_nama'));
/*if (isset($_GET['term'])){
$q = strtolower($_GET['term']);
$this->absensi_m->get_umat($q);
}*/
}
This is my model :
public function get_umat($word) {
$this->db->select('nama', 'tanggal_lahir');
$this->db->like('nama', $word);
$query = $this->db->get('msumat');
if($query->num_rows() > 0)
{
foreach($query->result_array() as $row)
{
$new_row['label'] = htmlentities(stripslashes($row['nama']));
$new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir']));
//build array
$row_set[] = $new_row;
}
echo json_encode($row_set);
}
}
And this is my javascript :
<script type="text/javascript">
$(function(){
$("#txt_nama").autocomplete({
source: "autocomplete"
});
});
</script>
I tried to inspect the javascript by using firefox's firebug and GC's developer tool, and this is what i got :
<input type="text" id="txt_nama" name="txt_nama" class="ui-autocomplete-input" autocomplete="off">
Notice that the autocomplete is off. I guess this is the problem so i tried to turn it on by adding this code :
$(document).ready(function() {
$("#txt_nama").attr("autocomplete", "on");
});
The autocomplete element is turned on when i add this code, but the autocomplete is still not working.
I also tried to use echo, but none of my echo is working :
if($query->num_rows() > 0)
{
echo num_rows();
echo 'a';
foreach($query->result_array() as $row)
{
$new_row['label'] = htmlentities(stripslashes($row['nama']));
$new_row['value'] = htmlentities(stripslashes($row['tanggal_lahir']));
//build array
$row_set[] = $new_row;
}
echo json_encode($row_set);
//return row_set;
}
What am I missing?
NOTE :
I just wondering about Routes, is it related to this error?because normally, people use controller/method in source: (JavaScript), but I can't do that because the generated route will have double controller (index.php/absensi/absensi/autocomplete), so I remove the controller and just use the method (source: "absensi")