Updated answer with help from hackre, christian.thomas, olimortimer :
The problem : I wasn't filtering.
So how I solved it :
Jquery :
$(function() {
var cct = $.cookie('ccn');
$( "#searchscholarship" ).autocomplete({
source: function(request, response) {
$.ajax({
url: '/global/c_ajax/ajax_scholarshipNames',
type: "post",
data: {term: request.term, 'ctn': cct},
dataType: "json",
success: function(data) {
response( $.map( data.myData, function( item ) {
return {
label: item.scholName,
value: item.scholID
}
}));
}
});
},
minLength: 3,
});
});
Big issue with AJAX and CI was the CSRF component, had to do some google searching on the "accepted" method of sending the token.
Then that sends to my c_ajax controller :
public function ajax_scholarshipNames()
{
$this -> load -> model('m_ajax');
$post = $this -> input -> post();
$data = $this -> m_ajax -> scholarships($post);
foreach ($data as $d) {
$value['myData'][] = array(
'scholID' => $d["intScholarshipID"],
'scholName' => $d["txtScholarshipName"]
);
}
$this -> output -> set_header('Content-Type: application/json; charset=utf-8');
echo json_encode($value);
}
Then in the model :
public function scholarships($post) // I usually don't use model just for database transactions but in this use case thats all I need the model to do.
{
$name = '%' . $post["term"] . '%';
$sql = "SELECT * FROM tableScholarship WHERE txtScholarshipName LIKE :name";
$ajax_schol = $this -> db -> conn_id -> prepare($sql);
$ajax_schol -> bindParam(":name", $name);
$ajax_schol -> execute();
return $ajax_schol -> fetchAll(PDO::FETCH_ASSOC);
}
Again thank you!
ORIGINAL POST
I am trying to set up auto complete and for the most part I've done just fine, except now I am trying to tweak the remaining few issues with it.
Currently what is bothering me is that when I type for example "Scholarship" (which is a fake name of a scholarship in my DB) "fewfew" also appear (which is another name)
If I type S alone Scholarship should be the only thing that shows not "fewfew"
My Jquery :
$(function() {
$( "#searchscholarship" ).autocomplete({
source: function(request, response) {
$.ajax({
url: '/global/c_ajax_controller/ajax_scholarshipNames',
data: {term: request.term},
dataType: "json",
success: function(data) {
response( $.map( data.myData, function( item ) {
return {
label: item.scholName, // Here it will either be Scholarship or fewfew
value: item.scholID // 8 or 9
}
}));
}
});
},
minLength: 1,
});
});
my Php function in the ajax controller :
public function ajax_scholarshipNames()
{
$data = $this -> m_global -> db_data($where = array(), $table = "tableScholarship");
foreach ($data as $d) {
$value['myData'][] = array(
'scholID' => $d["intScholarshipID"],
'scholName' => $d["txtScholarshipName"]
);
}
$this -> output -> set_header('Content-Type: application/json; charset=utf-8');
echo json_encode($value);
}
If it helps I am also using Codeigniter with CSRF turned ON however it doesn't seem to be affecting the outcome since the form I am using is using form_open :
<div id="editscholarship">
<?php
if (!$this -> session -> userdata('scholarshipID')) {
echo form_open('/global/c_scholarshipmaintance/editscholarship/' . urlencode('1'));
echo 'Please Enter Scholarship Name Below : <br>' . form_input('findScholarship', '', 'autocomplete="off", id="searchscholarship"');
}
?>
</div>
Thanks for your time.
Edit :
A picture of the respone(s) I am getting