Welcome Guest, Not a member yet? Register   Sign In
Calling a CI library from a javascipt?
#1

[eluser]Ray Julich[/eluser]
I am adding an AJAX autocomplete to my CI site, which uses a javascript include. Inside the javascript, it includes a .php file.
Code:
this.ajaxTarget = 'Autocomplete.php';
I copied this javascript from a site on the web and adapted is to my needs. The problem I've run into is that to get the data I need in the Autocomplete.php file, I need access to CI session variables and to a CI library. Is there a way for a javascript to get access to those CI files?
#2

[eluser]smilie[/eluser]
Hm, if it is .js file, I wouldn't know.

I also use (sort) of Ajax (RESTFull API), but my JS code is withing .php file, therefore I can do:

var somesession_data = "<?=$this->session->userdata('bla');?>";

But from .js file I wouldn't know...

Cheers,
Smilie
#3

[eluser]Krzemo[/eluser]
Make request to an url and let controller get your data in any of those: json, xml, html, text etc.
#4

[eluser]jalalski[/eluser]
I have a controller (application/controllers/admin/customers.php) that has a bunch of methods for working with customers. One of the methods is 'find_like' which then calls a search on the Customers model and returns json_encoded string to the caller:
Code:
class Customers extends Controller {
  ....
  ....
  function find_like($param='') {
    $param = $this->input->post('term');
    log_message('message', 'ADMIN: customers::find: ' . $param);
    $this->load->model('Customers_model', 'Customers');
    $matches = $this->Customers->find_like($param);
    print json_encode($matches);
    return true;
  }
}

This function is then available to the caller as:
index.php/app/admin/customers/find_like/

Remember to tell ajax to use POST, not GET for the autocomplete.
#5

[eluser]LuckyFella73[/eluser]
Derek Allard did a nice tutorial how to implement autocomplete
in a CI website:
http://video.derekallard.com/

Under "Downloads" you can even get the complete code and adjust to
your needs.

It is based on: Scriptaculous and prototype btw.




Theme © iAndrew 2016 - Forum software by © MyBB