views:

31

answers:

1

I'm using some Javascript I found from a post on StackOverflow. When I start entering text into the input, a spiny loading graphic appears within the input element, however nothing actually appears.

When I go to my URL that returns the JSON encoded string just to test it, it seems to work fine:

http://xxxxxxxx/app/get_clients/test (get_clients is the function, test is the string to search for)

Returns:

["Testing","Testing1","test11","test4","Testing21","Just Testing","testy"]

Any idea what I'm doing wrong?

Here is my JavaScript:

format_item = function (item, position, length)
{ 
 return item.title; 
} 

prep_data = function(data)
{ 
 tmp = $.evalJSON(data); 
 parsed_data = []; 
 for (i=0; i < tmp.length; i++)
 { 
 obj = tmp[i]; 
  parsed_data[i] =
  { 
  data: obj , 
  value: obj.isbn13, 
  result: obj.title 
  }; 
 }

 return parsed_data 
}

$(document).ready(function()
{
 $("#file_hide").hide();

 $("#<?php echo $this->form_name['company']; ?>").autocomplete({ 
  url : "<?php echo site_url("app/get_clients"); ?>", 
  parse: prep_data, 
  formatItem: format_item, 
 }); 
});

And my PHP:

 function get_clients($s)
 {
  $this->db_common->like('name', $s);
  $query = $this->db_common->get('clients');

  $results = array();

  foreach ($query->result_array() as $row):
     $results[] = $row['name'];
  endforeach;

  echo json_encode($results);
 }
A: 

Demo page has link to sample php backend implementation.

http://jquery.bassistance.de/autocomplete/demo/

In short, you need to use request parameter (q by default).

Nikita Rybak
Hmmm. I re-wrote my PHP to be similar to the demo, but still no luck. Are you sure it isn't my JavaScript that's messed up? My PHP script returns a JSON encoded string. Not sure what a request parameter would be.
dallen
@dallen Can you show html without php scripts (as it is in the browser)? Also, are there any errors in javascript console?
Nikita Rybak