在codeigniter中处理ajax表单时在视图中呈现视图?

时间:2013-02-08 11:14:52

标签: php codeigniter

在处理一个带有Ajax调用我的控制器功能的表单时,它会在div中显示通知我需要显示此通知消息,该消息已包含在具有特殊css格式的设计中..
我虽然可以通过渲染通知视图来完成,我将其从主设计中分割出来 我试着这样做..

public function add ()
    {
        $data = array();
        if (!empty($_POST)) {
            $this->model->insert();
                    $this->load->view('dashboard/notification', $data);
            exit;
        }
        $this->load->view('dashboard/categories', $data, FALSE);
    }

但遗憾的是它没有用,也没有出现..
当我打印一条简单的信息,表明一切正常时,它会显示信息并正常打印..

public function add ()
        {
            $data = array();
            if (!empty($_POST)) {
                $this->model->insert();
               echo 'message';
                exit;
            }
            $this->load->view('dashboard/categories', $data, FALSE);
        }

这是jquery脚本

$(document).ready(function() { 
     $("#addCatgory").validate({
         rules: {
                categoryname: {
                   required: true,
                   minlength: 5
                },
                categoryslug: {
                   required: true,
                   minlength: 5
                },
                categorytype: {
                   required: true,
                   min : 1
                }
          },        
          messages:{
               categoryname : 'Please enter your name',
               categoryslug : 'Please enter a valid email',
               categorytype : 'Please enter a valid type'
          },
          submitHandler: function(form) {
               $(form).ajaxSubmit({
                   type: "POST", 
                   async: false,  
                   data: $("#addCatgory").serialize(),
                   success: function(data){
                       $('#result').html(data);
                   },
                   error: function(){alert('error');}
                });
              }                                       
          });
   });

1 个答案:

答案 0 :(得分:1)

您需要通过将TRUE设置为第三个参数来修改此部分,以便将HTML打印回您的Ajax响应

public function add ()
    {
        $data = array();
        if (!empty($_POST)) {
            $this->model->insert();
                    echo $this->load->view('dashboard/notification', $data, TRUE);
            exit;
        }
        $this->load->view('dashboard/categories', $data, FALSE);
    }