控制器:test.php的自動使用完整的搜索MySQL的AJAX笨不工作
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url', 'captcha', 'email'));
$this->load->model('Fetch_data');
}
public function index()
{
$this->load->view('index',$data);
}
public function lookup()
{
$keyword = $this->input->post('term');
$data['response'] = 'false';
$query = $this->Fetch_data->lookup($keyword);
if(! empty($query))
{
$data['response'] = 'true';
$data['message'] = array();
foreach($query as $row)
{
$data['message'][] = array(
'college_name' => $row->college_name
);
}
}
if('IS_AJAX')
{
echo json_encode($data);
}
else
{
$this->load->view('index',$data);
}
}
}
view.php
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/base/jquery-ui.css" type="text/css" media="all" />
<link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js" type="text/javascript"></script>
<style>
.ui-menu {
list-style:none;
padding: 2px;
margin: 0;
display:block;
}
.ui-menu .ui-menu {
margin-top: -3px;
}
.ui-menu .ui-menu-item {
margin:0;
padding: 0;
zoom: 1;
float: left;
clear: left;
width: 100%;
font-size:80%;
}
.ui-menu .ui-menu-item a {
text-decoration:none;
display:block;
padding:.2em .4em;
line-height:1.5;
zoom:1;
}
.ui-menu .ui-menu-item a.ui-state-hover,
.ui-menu .ui-menu-item a.ui-state-active {
font-weight: normal;
margin: -1px;
}
</style>
<script type="text/javascript">
$(this).ready(function() {
$("#colleges").autocomplete({
minLength: 1,
source:
function(req, add){
$.ajax({
url: "<?php echo base_url(); ?>index.php/test/lookup",
dataType: 'json',
type: 'POST',
data: req,
success:
function(data){
if(data.response =="true"){
add(data.message);
console.log(data);
}
},
});
},
});
});
</script>
<input type="text" name="colleges" id="colleges" class="form-control" placeholder="Colleges and Universities" />
<ul>
<div class="well" id="result"></div>
</ul>
型號:
public function lookup($keyword)
{
$this->db->select('college_name,state,field')->from('all_colleges');
$this->db->like('college_name',$keyword,'after');
$this->db->or_like('state',$keyword,'after');
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
在這段代碼我想創建自動填充建議框。現在,我正在創建具有功能名稱查找的測試控制器,這是查看頁面中腳本調用的。在這裏,當我在文本框中寫東西時,它看起來像。
我怎樣才能解決這個問題?
當我寫東西在文本框中,它看起來像上圖@Naga中所示。請看一次。 – omkara
似乎沒有數據。只需發送條款價值作爲JSON編碼。不是消息,回覆等,我也編輯了我的答案。請檢查,並讓我知道如果仍然問題仍然存在... – Naga
我不能正確理解你@Naga請你編輯我的腳本,我可以正確理解。 – omkara