I am writing a Codeigniter app, and I use ajax to send data to my controller/method, and would like to know how to refresh a div using ajax without reloading the entire page (the path from db to view using ajax is still confusing for me)
I use this code to send data to the database
<script>
$(function(){
$("#rate").submit(function(){
dataString = $("#rate").serialize();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>product/rate",
data: dataString,
});
</script>
The posting to the db works just fine.
Then I have a div in the view that will get the result from the database so I append the code like :
<script>
$(function(){
$("#rate").submit(function(){
dataString = $("#rate").serialize();
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>product/rate",
data: dataString,
dataType: "html",
success: function(data){
$('#result').html(data);
}
});
return false; //stop the actual form post !important!
});
});
</script>
but nothing happens (I followed some snippets on the net)
Can anyone guide me through ?
Thanks
UPDATE //Controller
function rate(){
if ($this->input->post('e1') || $this->input->post('e2') || $this->input->post('e3') || $this->input->post('e4') || $this->input->post('e5'))
{
$this->Mproduct->rateProduct();
$this->db->cache_delete_all();
}
}
//Model
public function rateProduct()
{
$data = array('usage' => $_POST['e1'],
'packing' => $_POST['e2'],
'size' => $_POST['e3'],
'recycling' => $_POST['e4'],
'material'=>$_POST['e5'],
'idUser'=>$_POST['idUser'],
'idProduct' => $_POST['idProduct']
);
$this->db->insert('Rating', $data);
}