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関数

$.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 に答える