我想在codeigniter框架中使用jQuery自动完成多个字段我提到了this tutorial.But我的搜索文件没有工作。文本字段没有显示自动完成列表 这是我的代码,请帮我解决这个问题。
视图 .....................
<div class="row">
<form action="" name="students" method="post" id="students">
<input type="text" name="patientId" id="patientId_1" class="ui-autocomplete-input">
<input type="text" name="firstname" id="firstname_1" class="ui-autocomplete-input">
<input type="text" name="nic" id="nic_1" class="ui-autocomplete-input">
<input type="text" name="telephone" id="telephone_1" class="ui-autocomplete-input">
</form>
</div>
的jQuery .......
$('#patientId_1').autocomplete({
source: function( request, response ) {
$.ajax({
url : 'http://localhost/cafdc/BillingController/test',
dataType: "json",
data: {
name_startsWith: request.term,
type: 'patient_table',
row_num : 1
},
success: function( data ) {
response( $.map( data, function( item ) {
var code = item.split("|");
return {
label: code[0],
value: code[0],
data : item
}
}));
}
});
},
autoFocus: true,
minLength: 0,
select: function( event, ui ) {
var names = ui.item.data.split("|");
$('#firstname_1').val(names[1]);
$('#nic_1').val(names[2]);
$('#telephone_1').val(names[3]);
}
});
控制器 .......................
public function test()
{
$data=$this->Billing_Model->get_data();
echo json_encode($data);
}
模型 ....................
public function get_data()
{
if($_POST['type'] == 'patient_table'){
$row_num = $_POST['row_num'];
$result =$this->db->query( "SELECT patientId, fname, nic, tpnumber FROM tblpatient where name LIKE '".strtoupper($_POST['name_startsWith'])."%'");
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['patientId'].'|'.$row['fname'].'|'.$row['nic'].'|'.$row['tpnumber'].'|'.$row_num;
array_push($data, $name);
}
}
}
答案 0 :(得分:1)
您的代码无法正常工作因为您不是return
来自型号的任何数据
public function get_data()
{
if($_POST['type'] == 'patient_table'){
$row_num = $_POST['row_num'];
$result =$this->db->query( "SELECT patientId, fname, nic, tpnumber FROM tblpatient where name LIKE '".strtoupper($_POST['name_startsWith'])."%'");
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['patientId'].'|'.$row['fname'].'|'.$row['nic'].'|'.$row['tpnumber'].'|'.$row_num;
array_push($data, $name);
}
return $data;// return your data
}
}
答案 1 :(得分:0)
您必须在ajax调用中设置$('#topSearchButton').on("click", function () {
$('li:not([type="Documents"],:contains(Documents))',$(this).next()).toggle("fast");
});
,否则会将其设为type:"POST"
,因此请在GET
调用中添加type
,例如,
$.ajax