0

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);
    }
4

3 回答 3

1

作为对您“什么都没有发生”的问题的间接回答,我建议您采取一种策略,以确保您至少看到发生了一些事情。

  1. 除了success:处理程序之外,还要添加一个error:处理程序与一个alert()或一些东西。
  2. 在您的成功处理程序中,添加一条debugger;语句作为第一行。这将导致您的浏览器(chrome,当开发区域可见时)暂停执行。这样您就可以检查data变量的内容。


    $.ajax({
       type: "POST",
       url: "product/rate",
       data: dataString,
       dataType: "html",
       success: function(data){
            debugger;
            $('#result').html(data);
            },
       error: function() { alert("oops..."); }
       });

更新:无论如何,您rate()的控制器成员应该返回一些有效的html,但不是直接通过echo语句而是通过视图,如此处所述:
Codeingiter, manual, static pages



    function rate()
        {
        //...
        $this->load->view('rate/view', $data);
        }

假设您有一个文件application/views/rate/view.php



    <?php
    echo 'TODO: put some html code...';

于 2013-06-14T21:38:08.103 回答
1

尝试将一些数据输出到 rate() 函数中,例如:

echo "<b>Response to return</b>";
于 2013-06-14T21:43:34.773 回答
0

要摆脱 ob_start() 错误,请添加$config['compress_output'] = FALSE;config.php 或通过代码将其设置为 false:

$this->config->set_item('compress_output', FALSE); 
于 2013-06-14T23:38:03.273 回答