My controller get datas from database model, and sends them to view. In view i do foreach loop to draw divs to each entity. Every single div has a form element to rate its entity.
Controller:
function index(){
$data['main_content'] = 'list';
$data['entities'] = $this->database_model->getEntities();
$this->load->view('template', $data);
}
function postRate(){
$rate = array(
'entity_id' => $this->input->post('entity_id'),
'owner_id' => $this->input->post('owner_id'),
'value' => $this->input->post('value')
);
$this->database_model->persistRate($rate);
$this->index();
}
Model:
function getEntities(){
return $this->db->get('entity')->result();
}
function persistRate($rate){
return $this->db->insert('rate', $rate);
}
View:
<?php foreach($entities as $entity) : ?>
<div class="entity">
<?php echo form_open('entities/postRate/', $attr); ?>
<?php echo form_hidden('entity_id', $entity->id); ?>
<?php echo form_hidden('owner_id', $userid); ?>
<?php echo form_hidden('value', 1); ?>
<?php echo form_submit('submit', '+1'); ?>
<?php echo form_close(); ?>
</div>
<?php endforeach; ?>
In this case after rating, entire page will be reloaded but i want to refresh only that div where form was submited. I know javascript or jQuery can do it, ..
UPDATE
.. but i don’t know their logics, how will they know the exact div or the exact (updated) data of actual entity ? Shall i get it from database? i’m just confused with js/jquery/ajax.
It is really simple and can be found here: http://api.jquery.com/jQuery.ajax/
Here is an example:
Your controller method should receive a post variable called
id.That is a basic example, you really should read the link and learn more about it. 😉