2013-01-11 40 views
1

我是新代碼點火器,所以我不知道該怎麼做。所以我的問題是我有一個我從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

因爲它是一個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); 

對於上述步驟,你需要在你的AJAX功能來定義

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(); 
+0

我不知道如何我在腳本中添加JSON成功消息... PLX addjust你的代碼我的代碼,所以我可以查看或正確理解 – mynameisjohn

+0

我編輯了我的代碼。只需檢查一下 –

相關問題