Welcome Guest, Not a member yet? Register   Sign In
Autocomplete with JQuery and CI
#1

[eluser]Nick_2010[/eluser]
Hi,
I am new to CI. I have been struggling for past hour or so to use latest JQuery Autocomplete API with CI directly. http://docs.jquery.com/UI/Autocomplete

Here as you can see the source: can contain a URL.

Here is my controller functions - the first one calls the form and second is supposedly to be called by Ajax.

---
function autocomplete() {
$this->load->view('autocomplete_view.php');
}

function suggestions() {
echo "['apple', 'applet', 'banana', 'bandana']";
}
---

Here is my complete view

<link type="text/css" href="<?php echo base_url(); ?>js/css/ui-lightness/jquery-ui-1.8.4.custom.css" rel="stylesheet" />
[removed][removed]
[removed][removed]
[removed]
$(function() {
$("#search").autocomplete({
source: "<?php echo base_url(); ?>index.php/users/suggestions",
width: 300,
max: 10,
delay: 100,
cacheLength: 1,
scroll: false,
highlight: false,
type: 'POST',
});
});
[removed]
<form>

--

Now when I type in the search box the right controller and function is promptly called but I am actually getting a 404 error as per Apache logs. Not sure where could the problem lie. This is latest CI as of yesterday (1.7.2) and latest JQuery 1.4.2.

Any help will be greatly appreciated.

Thanks
#2

[eluser]intractve[/eluser]
When using AJAX to call the source the script expects JSON data in return, and JSON data is wrapped in curly brackets and not square ones.

you can get JSON data by
Code:
$arr = array('apple’, ‘applet’, ‘banana’, ‘bandana'); //can also be a result set from mysql.

echo json_encode($arr);
#3

[eluser]Nick_2010[/eluser]
Thanks. I used json_encode, however, the problem was with the way Ajax queried the controller.

Code:
"GET /ci/index.php/users/suggestions?term=a HTTP/1.1"

As you see it is adding a query parameter to the GET request URL "term=a", this is coming from Autocomplete module of JQuery.
This was not finding the right controller and so I was getting a 404 response.

After reading the past forum posts I changed the config to the following

Code:
$config['enable_query_strings'] = TRUE;
$config['uri_protocol'] = 'PATH_INFO';
#$config['controller_trigger']  = 'c';
#$config['function_trigger']    = 'm';
#$config['directory_trigger']   = 'd';


Note I enabled the query string and set the uri_protocol to PATH_INFO.

With this I am getting the right controller and function invoked with query string, also the clean URLs are working fine.
However, I am not sure if everything is fine because as per the user guide if I enable query strings then some helpers may not work.

Can someone please suggest if this is the right approach?

Thanks
#4

[eluser]pickupman[/eluser]
Don't be afraid to use the search tool. Check out response in another thread. [url="http://ellislab.com/forums/viewreply/745033/"]jQuery UI Autocomplete[/url].




Theme © iAndrew 2016 - Forum software by © MyBB