[英]CodeIgniter - Refresh div after submit form
My controller get datas from database model, and sends them to view. 我的控制器从数据库模型获取数据,并将其发送给视图。 In view i do foreach loop to draw divs to each entity.
鉴于我做了foreach循环来绘制div到每个实体。 Every single div has a form element to rate its entity.
每个div都有一个表单元素来对其实体进行评分。
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. 在这种情况下,评分后,整个页面将被重新加载,但是我只想刷新提交表单的那个div。 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 ?
我知道javascript或jQuery可以做到,..更新..但我不知道他们的逻辑,他们怎么知道确切的div或实际实体的确切(更新)数据? Shall i get it from database?
我可以从数据库中获取吗? i'm just confused with js/jquery/ajax.
我只是与js / jquery / ajax混淆。
It is really simple and can be found here: http://api.jquery.com/jQuery.ajax/ 它真的很简单,可以在这里找到: http : //api.jquery.com/jQuery.ajax/
Here is an example: 这是一个例子:
$.ajax({
url: "http://localhost/your_site/controller/method/",
type: "POST",
data: {id : 123},
dataType: "html",
success: function(data) {
$('.result').html(data);
}
});
<div class="result">
//YOUR AJAX RESULT WILL BE HERE
</div>
Your controller method should receive a post variable called id
. 您的控制器方法应收到一个名为
id
的后置变量。
That is a basic example, you really should read the link and learn more about it. 这是一个基本示例,您确实应该阅读链接并了解更多信息。 ;)
;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.