I am creating a website, which is a one-page website that interacts with the server via Ajax in CodeIgniter. General coding is of the following type:
controller (user.php) :
public function get_user_content() { $id = $this->input->post('id'); $hits = $this->user_model->user_data($id); $s = ''; foreach ($hits as $hit) { $s .= $hit->name; $s .= $hit->age; } echo $s; }
model(user_model.php) :
function user_data($id) { //do sql operation return $query->result(); }
View:
... ... <a href="#" id="23" class="user-data">Click here for user details</a> ... ...
JavaScript:
('.user-data').click(get_user_data); .... .... function get_user_data(response) { return $.ajax({ type: "POST", url: "<?php echo base_url();?>index.php/user/get_user_content", data: { id: this.id }, success: function(response) { $("#somediv").append(response); $(".someclass").click(another_function); }, error: function(error) { alert("Error"); } }); }
So, looking at the above javascript, there are separate functions for all actions that send some data to the server, and the specific html content is updated via Ajax .
I had the following questions (I'm just new to this):
1. Is there any better way of doing ajax in javascript than my implementation. 2. I'm not using the concept of views in CodeIgniter. I just `echo` results through my controller functions that gets embedded in javascript. This is because I want dynamic update in my app. It is a single page and there is no concept of new-page/new-tab. Is there any better way?
I do not know about any open source projects that could simplify / optimize.
javascript jquery ajax php codeigniter
xan
source share