Welcome Guest, Not a member yet? Register   Sign In
How to begin with ajax?
#1

[eluser]Buso[/eluser]
I know the basics, but I would like to learn good practices.
I don't know how to make php + ajax code in a way that if the client browser doesn't have jss enabled, I can still show him the login, for example (well, I know how to, but not in a good way.. i wanna use the full power of CI and say, Jquery)
I mean I would like a link to the login form, which fades-in and out by ajax, but if jss isn't enabled, the login is still shown somehow.
#2

[eluser]Colin Williams[/eluser]
First, build everything without touching JS (and realize that this means certain behaviors aren't possible.) Then, you can come in later and start hijacking link clicks and form submits to fire your Ajax calls. It might help to download something like BambooInvoice and take a peek at its code, see how it handles Ajax.
#3

[eluser]Buso[/eluser]
should I write my ajax stuff right into my views? or should I write helpers for that?
#4

[eluser]gusst250[/eluser]
[quote author="Colin Williams" date="1258264248"]First, build everything without touching JS (and realize that this means certain behaviors aren't possible.) Then, you can come in later and start hijacking link clicks and form submits to fire your Ajax calls. It might help to download something like BambooInvoice and take a peek at its code, see how it handles Ajax.[/quote]

I suppose, with BambooInvoice, you mean a javascript library like jQuery (which I'm familiar with)
Just asked because I'm facing the same problem.
#5

[eluser]Colin Williams[/eluser]
BambooInvoice is a CI application developed by EllisLab employee Derek Allard. It is a good application for new CodeIgniters to check out to see how he handles certain obstacles that you will face in all applications you develop (obstacles that typical PHP developers face before adopting a framework like CI. Yes, that's a shot at newbies...).
#6

[eluser]bretticus[/eluser]
[quote author="Buso" date="1258270713"]should I write my ajax stuff right into my views? or should I write helpers for that?[/quote]

Your AJAX scripting should be an abstraction exactly like views, models, and controllers are abstracted from each other in CI. Your PHP and javascript boundaries should never cross. Think of AJAX as another browser that javascript controls. AJAX is nothing more than javascript with a class called XMLHttpRequest that just happens to let your code act like you with a browser.

So, per Collins advice, make all your forms like you normally would (remember...AJAX is only another abstraction) and then insert your AJAX javascript code AFTER into the pages where you need them (Put your AJAX code in javascript files. No need to code anything about AJAX specifically in PHP.) The beauty of doing this with jquery is that you can use it to attach onclick and other events to DOM elements (of course you can do this with native javascript code instead of yet another framework, but jquery just makes it so much easier.) If javascript is disabled (think google bot) then the page works normally without interruption of service.

For a great example, watch this video tutorial from Jeffrey Way of Nettuts. Just a note, he uses the jquery built-in method $.ajax() in his example. I think you may find $.post() is a more streamlined method for posting data using AJAX.
#7

[eluser]Buso[/eluser]
[quote author="bretticus" date="1258289578"][quote author="Buso" date="1258270713"]should I write my ajax stuff right into my views? or should I write helpers for that?[/quote]

Your AJAX scripting should be an abstraction exactly like views, models, and controllers are abstracted from each other in CI. Your PHP and javascript boundaries should never cross. Think of AJAX as another browser that javascript controls. AJAX is nothing more than javascript with a class called XMLHttpRequest that just happens to let your code act like you with a browser.

So, per Collins advice, make all your forms like you normally would (remember...AJAX is only another abstraction) and then insert your AJAX javascript code AFTER into the pages where you need them (Put your AJAX code in javascript files. No need to code anything about AJAX specifically in PHP.) The beauty of doing this with jquery is that you can use it to attach onclick and other events to DOM elements (of course you can do this with native javascript code instead of yet another framework, but jquery just makes it so much easier.) If javascript is disabled (think google bot) then the page works normally without interruption of service.

For a great example, watch this video tutorial from Jeffrey Way of Nettuts. Just a note, he uses the jquery built-in method $.ajax() in his example. I think you may find $.post() is a more streamlined method for posting data using AJAX.[/quote]
thanks a lot




Theme © iAndrew 2016 - Forum software by © MyBB