我尝试使用从网站获得的代码在 codeignter 中进行自动完成。但这对我不起作用。任何人都可以找到问题
我使用的视图功能如下
<html>
<head>
<title>Autocomplete</title>
<script src="<?php echo base_url().'js/jquery.autocomplete.js'?>"></script>
<script src="<?php echo base_url().'js/jquery-1.6.4.js'?>"></script>
<script type="text/javascript">
$(function() {
$( "#username" ).autocomplete({ //the recipient text field with id #username
source: function( request, response ) {
$.ajax({
url: "http://localhost/autocomplete/index.php/autocontroller/search_username",
dataType: "json",
data: request,
success: function(data){
if(data.response == 'true') {
response(data.message);
}
}
});
}
});
});
</script>
</head>
<body>
<h1>Autocomplete</h1>
<input type="text" id="username" value="" />
</body>
</html>
我使用的控制器如下所示
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of autocontroller
*
* @author Sivam
*/
class autocontroller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->database();
}
function index()
{
$this->load->view('autoview');
}
function search_username()
{
$username = trim($this->input->get('term', TRUE)); //get term parameter sent via text field. Not sure how secure get() is
$this->db->select('fname');
$this->db->from('users');
$this->db->like('fname', $username);
$this->db->limit('5');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$data['response'] = 'true'; //If username exists set true
$data['message'] = array();
foreach ($query->result() as $row)
{
$data['message'][] = array(
'label' => $row->fname,
'value' => $row->fname
);
}
}
else
{
$data['response'] = 'false'; //Set false if user not valid
}
echo json_encode($data);
}
}
?>