将参数从控制器传递到代码点火器中的视图

时间:2013-01-11 07:39:01

标签: php jquery css ajax codeigniter

我是代码Igniter的新手,所以我不知道该怎么做。所以我的问题是我有一个我从ajax提交的表单。所以我想做的是当表单提交成功然后通知或css div类将出现在表单上方然后消失它。我不知道如何在从视图页面接受参数到控制器之后执行此操作我不知道如何发送参数控制器来查看或如何执行所有这些。这是我的控制器:

   class categoryController extends CI_Controller {


 function index(){

         $data['main_content'] = 'categoryView'; 
    $this->load->view('dashboardTemplate/template',$data); 

}


function addCategory(){

    //getting parameters from view 
    $data = array(
            'cat_name' => $this->input->post('cat_name')

    );

    $is_ajax = $this->input->post('ajax'); //or use this line


    $this->load->model('categoryModel'); 
    $query = $this->categoryModel->addCategories($data);


          if ($query && $is_ajax){            


         $page['main_content'] = 'categoryView';
         $page['v'] = '1'; // i dont know how this variable is not accessing in view page by echo $v 
         $this->load->view('dashboardTemplate/template',$page);


    }
    else
     {
        //
    }
}}

以下是我的观点:

     <?php 
   $attributes = array('id' => 'form-horizontal',
                       'class' => 'form-horizontal'       

        );
   echo form_open('categoryController/addCategory', $attributes);


$cat_name = array(
    'name' => 'cat_name',
    'id' => 'cat_name',
    'class' => 'cat_name');
 $button = array(
'name' => 'button',
'id' => 'btn',
'class' => 'btn btn-primary',
'value' => 'submit',
'type' => 'submit',
'content' => 'Submit'


     );
?>
 <h3>Add Category</h3>

 //here i want to do this .. that if form is submitted succesfully then this class will                        load only and the whole page remain the same 


         <div> class="alert-heading">sucess or not success!<div> 

 </div>
      <div class="control-group">
<label for="basicround" class="control-label">Category Name:</label>
 <div class="controls">
  <?php echo form_input($cat_name); ?>
<div class="form-actions">
  <?php echo form_button($button); ?></div>



<script type="text/javascript">
       $('#btn').click(function() { 

var cat_name = $('#cat_name').val();

if (!cat_name || cat_name == 'Name') {
    alert('Please enter Category Name');
    return false;
}

var form_data = {
    cat_name: $('#cat_name').val(),
    ajax: '1'       

};

$.ajax({
    url: "<?php echo site_url('categoryController/addCategory'); ?>",
    type: 'POST',
    data: form_data,
    success: function(msg) {
        $('#message').html(msg);
    }
});

return false;
  });

 </script>

1 个答案:

答案 0 :(得分:1)

由于它是Ajax提交,因此您需要将JSON数组从控制器传递到视图

echo json_encode($page);

控制器

$page['main_content'] = 'categoryView';
$page['v'] = '1'; // i dont know how this variable is not accessing in view page by echo $v 
echo json_encode($page);

对于上述步骤,您需要定义

data-type:JSON

在你的ajax函数中。

Ajax功能

$.ajax({
    url: "<?php echo site_url('categoryController/addCategory'); ?>",
    type: 'POST',
    data: form_data,
    data-type: "json",
    success: function(msg) {
        // Here you can access the values from controller like msg.v
        $('#message').html(msg);
    }
});

根据回复,您可以使用

显示成功消息
$(".alert-heading").show();