Welcome Guest, Not a member yet? Register   Sign In
how to use jquery autocomplete in codeigniter
#1

[eluser]goldensona[/eluser]
Hi

help me how to use jquery autocomplete in codeigniter i tried but the code didnt populate.

this is my view home.php

Code:
[removed][removed]
[removed][removed]
    <link rel="stylesheet" type="text/css" href="<?php echo base_url()."jquery-autocomplete/jquery.autocomplete.css"?>" />

[removed]//autocomplete function
    $().ready(function() {
     function formatItem(row) {
        return row[0] ;
    }
    function formatResult(row) {
        return row[0].replace(/(<.+?&gt;)/gi, '');
    }
    
        $("#country").autocomplete('searcheasy/view', {
        matchContains: true,
        minChars: 0,
        mustMatch: true,
        autoFill: true

    });
    
  
    });

this is my controller searcheasy.php

Code:
function view()
{
  $q = $this->input->post('country',TRUE);
        if (!$q) return;
  $this->load->database();
  $this->load->model('dbsearch');
  
     $data = $this->dbsearch->getdata(); //Search DB
    
        foreach($data as $row =>$key)
        {
           // if (strpos(strtolower($key), $q) !== false) {
                echo $key."\n";
         //   }
            
         }      
      $this->load->view('home',$data);
}



this is my model dbsearch.php

Code:
var $namearr=array();

function getdata() {

$data=$this->db->get('countrytable');
foreach ($data->result_array() as $row)
{
   $id= $row['id'];
   $name=$row['countryname'];
   $namearr=$this->array_push_assoc($namearr,$id,$name);
}
return $namearr;
}

function array_push_assoc($array, $key, $value)
{
$array[$key] = $value;
return $array;
}


i cant get the country list out in text box.suggest me to solve this.

thankyou
sona
#2

[eluser]mddd[/eluser]
Just some observations..

Code:
$this->load->model('dbsearch');
$data = $this->autocomplete->getdata(); //Search DB
You are loading model 'dbsearch' and then calling a method on 'autocomplete' ? Seems like that's not going to work.

Code:
$this->load->view('home',$data);
Why are you loading the home view here?? The output is going to be sent via Ajax to your script. It won't have any idea what to do with an entire web page! It just needs a list of autocomplete terms!

I would suggest checking what you actually get back from the Ajax call. It will probably have php errors and useless html in it.. Not something the autocomplete script knows what to do with.
#3

[eluser]goldensona[/eluser]
thanks mddd

i changed the model here
Code:
$this->load->model('dbsearch');
$data = $this->dbsearch->getdata(); //Search DB

i m new to codeigniter so can you help me how could i pass the data to home page through ajax .

my code didnt populate the list of terms returned by dbsearch


thanks
#4

[eluser]mddd[/eluser]
You don't need to do anything to "pass the data". The page searcheasy/view is already being loaded by Ajax!
All you need to do is output the data in a format that the jQuery autocomplete function expects. That's it.
Like I said: check what data is being output jQuery and you should see soon enough.
#5

[eluser]goldensona[/eluser]
thank you mddd

i remove the view loaded
Code:
$this->load->view('home',$data);

while i m enter letter in textbox it automatically disappear and it didnt populate the search list. is anything wrong in my jquery.

or my model.help me

thanks
#6

[eluser]mddd[/eluser]
You need to output in a format that jQuery understands. Right now you are just echoing every key on a new line.
Check the jQuery manual and find out in what format you have to output the data.
#7

[eluser]goldensona[/eluser]
thanks can u give me example for how to display the jquery data?
#8

[eluser]mddd[/eluser]
Browser extensions like Firebug for Firefox or Web inspector for Safari / Chrome will let you see all the loaded files and their contents.
#9

[eluser]goldensona[/eluser]
my textbox will trigger but content only doesnt populate for my controller which is posted previously how could i get the data
#10

[eluser]goldensona[/eluser]
how to pass the data to autocomplete jquery function from controller. can give some example please help me

thanks




Theme © iAndrew 2016 - Forum software by © MyBB