autocomplete in codeigniter
14,839
I am not able to understand why you use
$data['message'][] = array(
'label' => $row->fname,
'value' => $row->fname
);
You can try by single dimensional array.
$data['message'] = array(
'label' => $row->fname,
'value' => $row->fname
);
I have also use autocomplete by follwing this .http://www.jamipietila.fi/codeigniter-and-autocomplete-with-jquery/ Please try this...Its works for me ..Hopefully it will work for you as well.
Author by
Balu
Updated on June 04, 2022Comments
-
Balu almost 2 years
I tried to do autocomplete in codeignter using the code that I got from a site.But it is not working for me.Can anyone find the problem
The view function that I used is given below
<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>
And the controller i have used is given below
<?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); } } ?>