Welcome Guest, Not a member yet? Register   Sign In
Best AJAX solution for CodeIgniter?
#1

[eluser]KeyStroke[/eluser]
I need to use some AJAX functionality in my application. What's the best AJAX solution to use with CI in your opinion? shall I use an existing library or not?

I've checked the "AJAX for CodeIgniter" library and XAJAX, but they both seems to take so many steps to achieve simple results.

Any ideas?
#2

[eluser]xwero[/eluser]
ajax is a javascript related technology, it's based on a browser functionality, so i like to develop my ajax code in javascript.
#3

[eluser]frenzal[/eluser]
jquery will be release with the next codeigniter, you're free to use whatever library you want though
#4

[eluser]Vince Stross[/eluser]
I've been using jQuery. I like that I can do most AJAX functionality with a single - well-designed - javascript function. If you'd like a specific example please ask and I will paste something for you here.

One thing I will add to this thread as a question though is whether or not anyone else uses AJAX to call controller functions. I do this sometimes because it is quick and easy to just call a controller function and grab a view to return inside a DIV or something. However it seems silly to load all the CI code just to populate a DIV. Any thoughts on this?

What are best practices? Just run a straight-PHP file to interact with a database, or call a controller and use a model method?
#5

[eluser]KeyStroke[/eluser]
Well since you offered:
I have two drop-down boxes - one for "Category" and the other for "Sub-category". I want the user to pick a category, then the sub-category drop-down would show up with that category's sub-category.

The idea is simple, but I'm still having a hard time understanding how fetching and processing information from databases work in JS.
#6

[eluser]gon[/eluser]
Quote:One thing I will add to this thread as a question though is whether or not anyone else uses AJAX to call controller functions. I do this sometimes because it is quick and easy to just call a controller function and grab a view to return inside a DIV or something. However it seems silly to load all the CI code just to populate a DIV. Any thoughts on this?

I've never feel bad for getting AJAX content using CI controllers.
Actually that way you can validate data as if it wasn't sent via AJAX, or do whatever you want.

And you can use the same auth method for AJAX calls than for the rest of the site, so they will be as secure (or not) as the rest of the app.
#7

[eluser]barbazul[/eluser]
[quote author="frenzal" date="1207758124"]jquery will be release with the next codeigniter, you're free to use whatever library you want though[/quote]

What do you mean? Is JQuery going to become part of the core distribution of CI?
I haven't read anything about this... I definitely wouldn't want this to happen

About the thread: I always work with prototype an make my calls to "controller/function" urls.

sometimes I use the same controller/method to make the whole-page requests and to make the AJAX request. What I do in this cases is pass a parameter or even a request header sometimes, to specify the way I want the output to be formatted. That is: display the page view when the header is not present or simply echo json_encode($data) when it is present or even load a different view or something like that
#8

[eluser]Pascal Kriete[/eluser]
[quote author="barbazul" date="1207787672"]
What do you mean? Is JQuery going to become part of the core distribution of CI?
I haven't read anything about this... I definitely wouldn't want this to happen
[/quote]

Derek mentions it at the bottom of this post. I don't know how thrilled I am about it either. Not because it's included, but because it's one more reason for people not to learn javascript and make a mess of it.

If anyone can pull it off though, it's EL.

On topic: I pretty much do what barbazul does. I always extend a common controller, which checks if $_POST['ajax'] is set. If it is, I unset it and set a constant. With that constant set my header and footer are disabled so I have a clean response.
#9

[eluser]barbazul[/eluser]
Hey! now that I read about it I like the proposal!

I have an unfinished project that I started that sounds very similar to what they intend, that is, an extendable library to have many "special effects" done in the server and use drivers to switch between Mootools, JQuery, scriptaculous, Ricco or whatever....

That project, though, like many others, ended up getting nowhere because of lack of time and dedication. BUT if the CI team sets the basis for this I'll sure be a contributor to the cause.


P.S. Sorry I got completely out of topic
#10

[eluser]lookatthosemoose[/eluser]
I just discovered that you can load views which will serve as the response for the ajax request like this. I'm using mootools here.

Code:
$('add_new').addEvent('click',function(e){
   new Event(e).stop();
   new Ajax("<?=site_url('dashboard/addressbook/new')?>",{
    method :'post',
    data : 'client_id=' + 1,
    update : $('action')
   }).request();

controllers/dashboard/addressbook/new

Code:
function new(){
    $data['client_id'] = $this->input->post('client_id');
    $this->load->view('dashboard/addressbook/ajax/new',$data);
}

views/dashboard/adressbook/ajax/new
Code:
//whatever you want to output here will become the ajax response.
//In this case, the DOM node named 'action' will be updated with the contents of this view.
//because of the line "Update : $('action')" in the orig request.

Pretty nifty. it's a good way to keep from outputting HTML in your models or controllers.




Theme © iAndrew 2016 - Forum software by © MyBB