Use of ajax framework to develop a webapp employing php, codeigniter - php

I am going to start developing a webapp using php framework codeigniter. The app is going to do most of the database dealing using ajax/jquery. From what I know, I would be implementing the following steps to do a particular task
Create a view page
The events performed on view page's elements, i.e., click, mouseover etc. will be attached to event handler functions in js files included in the view
js functions will be making get, post requests to server side
In case some dynamic values need to be passed to js functions they'll passed using inline php code given below.
On server side, some database queries will be performed to generate a json(sometimes xml) which will be sent as response
Based on the response, the js function callback will manipulate the dom.
Now, my question is whether there is an ajax framework that can further simplify the implementation of steps given above. if not a framework, then may be a better approach to implementing ajax and php.
// This is part of view page
<a href="Delete User" onclick="deleteUser('<?php echo $userid; ?>')" />

Welcome to jQuery. This is not an AJAX framework (I have never heard of one of those) but it is a JavaScript Framework that incorporates some easy AJAX functionality.
Everyone talks about "integrating with the PHP framework!" like its some sort of crazy feature in PHP. JavaScript makes a request to a URL and does something with the response. That URL could be native PHP, CodeIgniter, a static file or bloody ColdFusion, it is all the same.
So if you are using CodeIgniter, in your view put:
$.get('/controller/method/param1/param2', function(data) {
$('div#someid').text(data.whatever);
}, 'json');

I don't really know what you mean by "AJAX framework" and whether you're talking about a clientside or serverside framework.
In the event you're looking for a clientside framework that handles client-server data synchronization, check out http://documentcloud.github.com/backbone/

Codeigniter has a javascript class that might help avoid "the Mess" you spoke of.
http://codeigniter.com/user_guide/libraries/javascript.html

Related

what php strategy to use to support frontend ajax calls?

I want to refactor my php site which supports a front end which makes ajax calls.
Currently my ajax calls a php page which stores all my backend code and based on the parameters passed from the ajaxcall it determines which php methods to run.
I realize building a rest API as the php backend woudl be better but was wondering if there is another alternative? ie a good way to structure a php page that allows for clean re-usable ajax calls from the front end?
Thanks
There are multiple ways of doing such kind of web development. It could be REST based micro framework approach in PHP where you invoke different views. Another could be to invoke the PHP code solely based on the input or interactions on the view (HTML/Javascript) using AJAX. Depends what you want to achieve.

How to get data form database without reloading the page?

I want to make a faq panel when user ask any question it show on the top of division without reloading the page for that i think i have to make a connection with database after every few second. Now the question is that how to make connection without reloading the page and how to show new question ?
You have two options:
Ajax, which allows you to retrieve data from the server with JavaScript, which you can then use to manipulate the DOM. The basis of Ajax is the XMLHttpRequest object, which allows you to retrieve data completely behind-the-scenes in JavaScript. Note that Ajax is limited by the Same Origin Policy, but for what you're describing, that's fine — you'll be loading data from the same origin.
Frames (e.g., iframe elements), which you can load content into by setting their src property.
Of the two, Ajax is much more flexible.
References / further reading:
DOM2 Core
DOM2 HTML
DOM3 Core
HTML5 Web Applications APIs
XMLHttpRequest object
Side note: Although obviously you can use XMLHttpRequest and the DOM methods directly, note that there are cross-browser differences (and outright bugs), which can be smoothed over for you by a good library like jQuery, Prototype, YUI, Closure, or any of several others. They also provide a lot of useful utility functionality, allowing you to focus on the actual problem you're trying to solve rather than the details of the plumbing.
For example, here's how you would send an Ajax request to a server using jQuery and have an element on the page updated with the HTML fragment the server sends back:
$("#target").load("get_the_data.php", {article: x});
That says: Request an HTML fragment from get_the_data.php sending it the parameter article with the value from the x variable, and put that HTML fragment inside the element that has the HTML id "target". That would be a good 10 lines of code if you didn't use a library. Now, that's not a lot, but repeat that over and over (and in the process deal with an IE bug around looking up elements by their id), and you see how it can add up.
I do recommend you read the references above so you know how the libraries are doing what they're doing (it's not magic), but there's no reason not to leverage the hard work people have done to make this stuff easier.
I think what you need is AJAX. It is a way of contacting the server with only partial page refresh. I don't know how it works with php as i use asp.net but here is a link that may help.
It's Ajax turn. What you need is a technology called AJAX. You can find something usable in jQuery library that provides different methods to work with Ajax more easily.
http://api.jquery.com/jQuery.ajax/
You will create a relation with your server-side programming language and your database through a client-side scripting language (Javascript in this case).
This is an example of these realations:
http://jqueryui.com/demos/autocomplete/
You have to look into push/publish/subscription technologies. Few that i know are pubnum and commet

Is there a MVC approach that uses PHP for Model and Controller and pure HTML for view while transporting data between them with JSON?

I just wondered if there is any existing framework or someone has experiences with using entirely static HTML templates that get filled with data provided via JSON from the server.
The HTML pages with JavaScript support should fulfill the role of the View whereas Controller and Model are handled by the PHP on the server side.
Views would the query via JSON-RPC or similar data from the Model and then render the results by manipulating the DOM.
I pretty much like this idea, it came into my mind weeks ago but I never found something on the web experiences with that or even already existing framework.
But maybe I am just a noob at googling?
Maybe one of you can help me out here.
Thanks in advance
Have a nice day!
Benjamin
This is very much possible and often found with Rich Client Applications. The first call to the application delivers the initial UI with any behavior defined in JavaScript files. Any further interaction is done via Ajax.
For the serverside, have a look at
Zend_Rest_Server
Zend_Json_Server
though this could just as well be a regular MVC application too. A Json or XML Response can still be considered a View. It's up to you to code your controllers to return appropriate data.
That's pretty much how Google do a lot of their stuff - do a view source on Gmail to see how much data they return in the HTML.
It's called Ajax. (That's not meant to sound too sarcastic - Ajax doesn't necessarily work like this, but ajax-based apps will do a lot of the rendering logic via JavaScript rather than via server-side code.)
You need a framework that doesn't limit you to any specific way of handling the view component of your MVC application and I think Zend Framework would be your best choice. It also has a great integration with DOJO and that makes it a lot easier to achieve what you need.

CakePHP integration with ExtJS 3

I am a PHP and ExtJS user. I am looking into developing an application using a good PHP framework (CakePHP; good as in "I consider this good for me") and ExtJS version 3. What I would like to achieve is a complete Ext viewport with many grids and functions that would call PHP urls for retrieving data, saving data, edit/remove data (not just for grids, also for treepanel and such). I would like to use CakePHP as backend with all its capabilities for executing these functions. My first goal is to integrate the obAuth component (or any other secure authentification plugin for CakePHP) with an ExtJS 3 login interface. I am searching for the best method of combining these too so that I can easily restrict functions based on the usergroup access. I am thinking of a setup where the logged in user makes one post from Ext regarding the execution of a function and the CakePHP response made present as errors or notifiers through Ext alert boxes.
Do you think this is possible ? Any thoughts of an ideal config for this ?
Thank you.
If you're going back and forth between JavaScript and Cake a lot the first thing you might want to do is override or extend the default View class so it'll package variables you set in the controller automatically into a JSON array or whatever you prefer. That'll save you from having to make a view for each action and/or overriding the layout each time. Study cake/libs/view/view.php and/or cake/libs/view/media.php, you can create custom views in app/views/.
Make use of the RequestHandler Component a lot to reuse logic both for normal views and AJAX.
Beyond that it's normal JS with a PHP backend.
This is actually very easy. First, integrate the obAuth Component into a basic CakePHP install, to see if it works properly and doesn't have any strange quirks. Once that's done, get to work on your frontend.
Your frontend should be designed entirely with ExtJS components. Either design your frontend with ExtJS or via HTML templates, but try not to do both, as it gets confusing and hard to maintain. I recently did this, and every controller action had a view that set up the DOM with some basic elements (a header for the page, any divs I needed to bind components to, and the .js file that was specific to that page/view).
Then, make your application RESTful. All of your ExtJS components can read data from a DataStore (or can just take a URL as the data source), so you just give them the paths you'd like and you're done.
Check out Bancha, it integrates ExtJS 4 and CakePHP 2.
It does this by doing all the communication in the background using an completely implemented Ext.Direct for CakePHP.
cheers
Roland

js libraries and ajax frameworks

what's the difference between using a js library like jquery and using an ajax framework what's the most active ajax framework out there( preferably for PHP)
or a better question would be...what's the difference between an ajax request fired with Jquery and an ajax request fired using any "AJAX frameworks"
As PHP frameworks typically sit on the web server and generate web pages that are then rendered by the client, a PHP "AJAX" framework is likely going to generate JavaScript code that is then executed by the web browser. The JS code that gets generated is serving the same role as JS you would write by hand to do the same thing, such as the jQuery.ajax() call you mention.
So, typically, the difference is between code written for you, and code you write by hand.
I think you are confused about what is Ajax and how it is related to PHP. Basically "Ajax" just means that some javascript code is making calls behind the curtains to some backend server to make an action or update something on the user interface.
That backend server language could be PHP, ASP, Ruby or anything really. Thats why the most popular "Ajax Frameworks" (Prototype, JQuery) are purely Javascript, and don't depend on you using PHP or anything.
On the other hand there's PHP Frameworks like CakePHP, CodeIgniter, Zend Framework (or the one you mentioned, Akelos) who just help you doing some tedious tasks in PHP, like templating, database access, etc. These frameworks don't depend on any "Ajax Framework" you can use any of them.
So just look at this separately, there's "PHP Frameworks" and there's "Javascript Frameworks" aka (badly) "Ajax Frameworks".
Of course there's always the exception to the rule, there's some project who make PHP calls to output Javascript code, like Xajax. But i would recommend you to avoid them or you will end up more confused.
what's the difference between using a
js library like jquery and using an
ajax framework like Akelos?
I believe Akelos is a PHP framework and NOT a Javascript framework.

Categories