Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm developing web application using PHP with CodeIgniter MVC framework with a huge real time client-side functionality needs. This is my first time to build large scale of client-side app. So I combine the PHP with a large scale of Javascript modules in one project.

As you already know, MVC framework seperate application modules into Model-View-Controller.

My concern is about View layer.

I could be display the data on the DOM by PHP built-in script tag by load some data on the Controller. Otherwise I could use AJAX to pulled the data -- treat the Controller like a service only -- and display the them by Javascript.

Here is some visualization

I could put the data directly from Controller:

<label>Username</label> <input type="text" id="username" value="<?=$userData['username'];?>" /><br />
<label>Date of birth</label> <input type="text" id="dob" value="<?=$userData['dob'];?>" /><br />
<label>Address</label> <input type="text" id="address" value="<?=$userData['address'];?>" />

Or pull them using AJAX:

$.ajax({
  type: "POST",
  url: config.indexURL + "user",                
  dataType: "json",
  success: function(data) { 
    $('#username').val(data.username);
    $('#dateOfBirth').val(data.dob);
    $('#address').val(data.address);
  }
});

So, which approach is better regarding my application has a complex client-side functionality?

In the other hand, PHP-CI has a default mechanism to put the data directly from Controller, so why using AJAX?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Ajax is used for fetching data from remote server(s) without page refresh. You can use to fetch data from servers asynchronously and hence the page will seems interactive. Your both approaches are fine and can work for you. But to explain you this, let me explain by an example.

You have page where you have 5 blocks, latest users, latest news, latest comments, recipes and offers. Each block has its own data. Now if you are using the approach to get all that data in controller and pass it to views it will work fine and will have no side effects of this approach.

On the other hand, if you use ajax asynchronous call, first the page will load, then some data loading indicator will be displayed by javascript and asynchronous calls will be made to server for all those blocks. When the data is loaded to the respective blocks, the loading indicator will be hidden. Now in this approach the page will be interactive and will look alive.

If you google, then you will find more reasons.

I hope this will help.

share|improve this answer
    
Thanks for great explanation. So, I should use AJAX for multitasking. But, does it seems Javascript is taking over my Controller layer in PHP. Is that somewhat redudant to have application Contoller layer both in Javascript and PHP? And how about the Javascript vulnerability? What's your opinion about that? :) –  wlz Jun 23 at 6:27
    
Yes in a sense, but not sure if it is multi tasking :P . I think you should read MVC, and jQuery ajax with CI. Ajax is not taking over your controller. You will still have to create controller actions (functions) which will be called by ajax. Ajax is only used to send http request seemless and without page refresh and then using DOM, insert data to your desired places. You can use some ajax libs like jQuery where they have handled security for you, and is easy to use for ajax calls with bundle of other features. –  altafhussain Jun 23 at 8:56
    
Thanks, so now I have Model-View-Controller, then what layer does the Javascript belongs to? I think because Javascript handles my application logic (eg. what happen when I click my button) and I'm doing some validation there, it belongs to Controller layer. –  wlz Jun 23 at 10:21
    
Please note that javascript is client side, it does not run on server, it runs in browser. JS can be wrote in view files or in separate JS files, so we can say it belongs to view side. The js validation is called client side validation, and php (or any other server side scripting language) validation is called server side validation. Please note that your application logic should be always handled by Controllers (Request Handling) and Models (Business Logic Handling ie database operations), not by JS. Js just provides some support to view. –  altafhussain Jun 23 at 11:18
    
This is a broad topic to discuss and you have to do some research on google about this. You can read more about MVC en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller and bennadel.com/blog/… . –  altafhussain Jun 23 at 11:20
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.