I’m want to create an autosuggest field using twitter bootstraps typeahead plugin. here is my current code. It returns 500 Internal Server Error in inspect elements console. I think the problem is in model. Please help me. Thanks in advance.
view: register
<script type="text/javascript">
$(function() {
$('#office').typeahead({
source: function(typeahead, query) {
$.ajax({
url: "<?php echo base_url('main/get_offices'); ?>",
type: "post",
data: "search=" + query,
dataType: "json",
async: false,
success: function(data) {
typeahead.process(data);
}
});
}
});
});
controller: main
public function get_offices() {
$this->load->model('offices');
$data = $this->offices->get();
echo json_encode($data);
}
model: offices
public function get() {
$office = $this->input->post('search');
$this->db->select('name');
$this->db->from('departments');
$this->db->like('name', $office);
$query = $this->db->get();
$office_array = array();
foreach ($query->result() as $row) {
$office_array = $row->name;
}
//$data['office'] = $office_array;
return $office_array;
}
I tried your code with typeahead in the function parameters but that doesn’t seem supported.
I rewrote your code to remove typeahead and return the result outside the scope of the ajax call.
The model function get seems to have a typo, at this part
It should be
$office_array[].