在 hackre、christian.thomas、olimortimer 的帮助下更新了答案:
问题:我没有过滤。
那么我是如何解决的:
查询:
$(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,
});
});
AJAX 和 CI 的大问题是 CSRF 组件,必须对发送令牌的“已接受”方法进行一些谷歌搜索。
然后发送到我的 c_ajax 控制器:
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);
}
然后在模型中:
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);
}
再次谢谢你!
原帖
我正在尝试设置自动完成,并且在大多数情况下我做得很好,除了现在我正在尝试调整剩下的几个问题。
目前困扰我的是,当我输入例如“奖学金”(这是我数据库中奖学金的假名)时,也会出现“少数”(这是另一个名字)
如果我单独输入 S 奖学金应该是唯一显示不是“少数”的东西
我的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,
});
});
我在 ajax 控制器中的 PHP 函数:
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);
}
如果它有帮助,我也会在 CSRF 开启的情况下使用 Codeigniter,但它似乎不会影响结果,因为我使用的表单正在使用 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>
谢谢你的时间。
编辑 :
我得到的响应的图片