Web Application: Combining View Layer Between PHP and Javascript-AJAX
- by wlz
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?