AJAX Autosearch
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function ajaxSearch() {
alert('hai');
var input_data = $('#search_data').val();
alert(input_data);
$.ajax({
type: "POST",
url: "search/auto_search",
data: {search_data:input_data},
success: function(data1) {
alert(data1);
// return success
if (data1.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data1);
}
}
});
}
</script>
CONTROLLER
public function auto_search() {
$search_data = $this->input->post('search_data');
//print_r($search_data); die();
$query = $this->search_model->autocomplete($search_data);
// print_r($query); die();
foreach ($query->result() as $row):
echo $row->uid ;
echo $row->name ;
endforeach;
}
MODEL
public function autocomplete($search_data) {
$this->db->select('name');
$this->db->select('uid');
$this->db->like('name', $search_data);
$dt = $this->db->get('tbl_reg', 10);
//print_r($dt); die();
return $dt->result();
}
当我在控制器中回显出search_data时,我得到了整个HTML页面的原因,请你为这个问题提供解决方案吗?
答案 0 :(得分:0)
也改变了ajax调用。
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function ajaxSearch() {
alert('hai');
var input_data = $('#search_data').val();
alert(input_data);
$.ajax({
type: "POST",
url: "/search/auto_search",
data: {search_data:input_data},
success: function(data1) {
alert(data1);
// return success
if (data1.length > 0) {
$('#suggestions').show();
$('#autoSuggestionsList').addClass('auto_list');
$('#autoSuggestionsList').html(data1);
}
}
});
}
更改控制器如下。
public function auto_search() {
$search_data = $this->input->post('search_data');
//print_r($search_data); die();
$query = $this->search_model->autocomplete($search_data);
// print_r($query); die();
$str='';
foreach ($query as $row):
$str.=$row->uid ;
$str.=$row->name ;
endforeach;
echo $str;
}
这将起作用,因为您已经在模型中返回结果,并且您不需要使用$ query-&gt; result();