2014-03-29 160 views
1

我想請一些幫助。我有這種形式的基團: 這是視圖:

<?php echo form_open('admin/posts/post/'.$post->id); ?> 
// other fields here... 
<div class="form-group"> 
       <label for="" class="col-sm-2">Visible</label> 

       <div class="col-sm-10"> 
        <div class="onoffswitch"> 
         <?php 
          $visible = ($post->visible) ? $post->visible : $this->input->post('visible'); 
          $visible_data = array(
           'class' => 'onoffswitch-checkbox', 
           'id' => 'visible', 
           'checked' => ($visible == '1') ? true : false, 
           'name' => 'visible', 
           'value' => ($post->visible) ? $post->visible : $this->input->post('visible'), 
           ); 
         ?> 
         <?php echo form_checkbox($visible_data); ?> 
         <label class="onoffswitch-label" for="visible"> 
          <div class="onoffswitch-inner"></div> 
          <div class="onoffswitch-switch"></div> 
         </label> 
        </div> 
       </div> 
      </div> 

// more fields ... 
<?php echo form_close(); ?> 

這是控制器

public function post($id){ 
     $this->data['post'] = $this->post_model->get($id); 

     $this->form_validation->set_rules($this->post_model->rules); 
     if ($this->form_validation->run() === true) { 

       var_dump($this->input->post('visible')); //not getting anything from the visible field 

      // store data in database and redirect 
      $this->post_model->save($id); 
     } 

     // load the view 
     $this->load->view('admin/post/edit'); 
    } 

Basicly這個作品作爲on/off switch button。問題是,當我點擊提交按鈕時,它不會將字段($this->input->post('visible')) 的$ _POST數據發送到我的模型,以便將其存儲在數據庫中。

任何想法有什麼不對,應該如何解決?

回答

1

這與Codeigniter無關,它只是瀏覽器的工作方式,如果複選框未選中,則不會將POST數據發送到服務器。

所以,你可以在控制器檢查$this->input->post('visible')(它會返回false,如果該複選框是unchecheck,無論您在value

或者你也可以做一個小的黑客,把一個隱藏的輸入具有相同名稱和值false你複選框前。

在你的榜樣,你應該把value="1"上的複選框,並有<?=form_hidden('visible', 0)?>複選框前。