I want to create jquery autocomplete with Codeigniter with data as below :
$data = Array
(
[0] => Array
(
[name] => Pencil
[id] => 111
)
[1] => Array
(
[name] => Paper
[id] => 112
)
[2] => Array
(
[name] => Stappler
[id] => 113
)
[3] => Array
(
[name] => Cutter
[id] => 114
)
)
My controller :
public function search_product() {
if (count($data > 0)) {
$json_array = array();
for ($s = 0; $s < count($data); $s++) {
$json_array[] = array("name"=>$data[$s]['name'], "id"=>$data[$s]['id']);
}
echo json_encode($json_array);
}
}
Javascript code :
$(document).ready(function () {
$(function () {
$( "#autocomplete" ).autocomplete({
source: function(request, response) {
$.ajax({
url: "<?php echo base_url('search_product'); ?>",
data: { bahasa: $("#autocomplete").val()},
dataType: "json",
type: "POST",
success: function(data){
response(data);
}
});
},
select: function (event, ui) {
$('#autocomplete').val(ui.data.name); // display the selected text
$('#code').val(ui.data.id); // save selected id to input
return false;
},
});
});
});
My view :
<div id="body">
Text: <input type="text" id="autocomplete" />
</div>
<div id="body">
Text: <input type="text" id="code" />
</div>
But autocomplete still not working.