使用codeigniter在搜索栏中自动完成功能

时间:2016-11-16 13:57:51

标签: javascript php jquery html codeigniter

每一个人。我正在尝试进行自动完成搜索,但它无法正常工作。 当用户在搜索栏中写入时,数据来自表格中的a列。 这是代码: 视图:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>


</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script>
$( "#country_id" ).autocomplete({
        source: function(request, response) {
            $.ajax({
            url: "http://localhost/new/index.php/travels/search_fields",
            data: { term: $("#country_id").val()},
            dataType: "json",
            type: "POST",
            success: function(data){
               var resp = $.map(data,function(obj){
                    return obj.tag;
               }); 

               response(resp);
            }
        });
    },
    minLength: 2
});
</script>

<form>
<input type="text" name="country_id" id="country_id" >                   
</form>
</body>
</html>

型号:

public function search_field($country_id){
$q= $this->db->query("select()->from('travels_detail')->where('destination', $country_id");
        echo json_encode($q->result_array());

    }

控制器:

public function search_fields(){
        $destination= $this->input->post('country_id');
        $this->travel->search_field($country_id);
}

1 个答案:

答案 0 :(得分:0)

在模型中,您的有效查询错误->get(),并且您在活动查询中遗漏了public function search_field($country_id){ $q = $this->db->select("*")->from('travels_detail')->where('destination', $country_id)->get(); echo json_encode($q->result_array()); }

跟随这样:

public function search_field($country_id){
    $this->db->select("*");
    $this->db->from('travels_detail');
    $this->db->where('destination', $country_id);
    $q = $this->db->get();
    echo json_encode($q->result_array());
}

,或者

$destination

在控制器而不是$country_idterm

您调用ajax data: { term: $("#country_id").val()}, POST值,如$this->input->post('country_id');,但您在控制器中调用$country_id = $this->input->post('term');

所以,在控制器public function search_fields(){ $country_id = $this->input->post('term'); $this->travel->search_field($country_id); }

所以,你的控制器是:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

//##### PUSH HTML FILE
app.get('/', function(req, res){
     res.sendFile(__dirname +'/index.html');
});

//##### START SERVER
http.listen(3000, '0.0.0.0', function(){
     console.log('started on port: 3000');
});

//##### USER CONNECTED
io.on('connection', function(socket){
     console.log('A User Connected');

//##### LOG MESSAGE
socket.on('chat message', function(msg){
    console.log(msg);
    io.emit('chat message', msg);
});

//##### LOG USERNAME
socket.on('set username', function(username){
    console.log('NEW USER: ' + username + ' has been set!');
    io.emit('set username', username);
});

//#####USER DISCONNECTED
socket.on('disconnect', function(){
    console.log('A user disconnected');
    });
});