1

我是代码 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>
4

1 回答 1

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({
    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();
于 2013-01-11T07:52:38.797 回答