2016-09-17 37 views
0

請幫我我想刪除一些複選框,如果複選框未選中。刪除條件,如果多個複選框在codeigniter中未選中

我的複選框是從數據庫我的SQL自動顯示。並且我想在複選框未選中時刪除一些複選框。 請幫助

其我的控制器

public function edit_overview($id_product) 
{ 
    if ($this->input->post('submit')) { 
     foreach ($id_overview = $this->input->post('id_overview') as $rm) { 
      $check_idoverview = $this->Biostar->check_idoverview($id_product, $rm); 
      if ($check_idoverview==unchecked){ 
       $data['file'] = $check_idoverview; 
       $this->Biostar->delete_overview($check_idoverview,$id_product); 
      }else{ 

      if ($check_idoverview > 0) { 

      } else { 
       $datafield = array(
        'id_product' => $id_product, 
        'id_overview' => $rm 
       ); 
       $this->Biostar->saveoverviewproduct($datafield); 
       $message_success = "Data Has Been Update"; 
      } 
      } 
     } 
    } 
    $data['message_success'] = @$message_success; 
    $field = $this->Biostar->get_overview($id_product); 
    $fieldid_product = $this->Biostar->get_id_product($id_product); 
    $data['field'] = $field; 
    $data['id_product'] = $fieldid_product; 
    $data['content'] = "biostar/edit_overview_product"; 
    $this->load->view('dashboard/index', $data); 
} 

我的模型

function delete_overview($check_overview,$id_product) 
{ 
    $sql = "delete from overview_briostar where id_overview='$check_overview' AND id_product='$id_product'"; 
    return $sql; 
} 

我查看

<form method="post" action="<?php echo base_url(); ?>biostar/add_overview_product/<?php echo $id_product->id_product; ?>"> 
      <div class="box-body"> 
       <?php foreach ($speed as $row){ ?> 
        <div class="checkbox"> 
         <label> 
          <input type="checkbox" name="id_overview[]" onClick="EnableSubmit3(this)" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?> 
         </label> 
        </div> 
       <?php } ?> 
       <!-- /input-group --> 
      </div> 
      <div class="box-footer"> 
       <input value="Submit" type="submit" id="submit3" name="submit" class="btn btn-primary"> 
      </div> 
       </form> 
+0

你是什麼意思刪除unchecked checkbox.please解釋清楚在這裏得到任何幫助 – Zeeshan

+0

emmm。如果複選框未選中,我想刪除它 –

+0

您刪除未選中的記錄? 你可以嘗試刪除那些從「所有記錄」中沒有發佈的'ids'。 – Zeeshan

回答

0

你可以嘗試這樣的

<form method="post" action="<?php echo base_url(); ?>biostar/add_overview_product/<?php echo $id_product->id_product; ?>"> 
     <div class="box-body"> 
      <?php foreach ($speed as $row){ ?> 
       <div class="checkbox"> 
        <label> 
         <input type="hidden" name="all_id[]" value="<?php echo $row['id_overview']; ?>" /> 
         <input type="checkbox" name="id_overview<?php echo $row['id_overview']; ?>" onClick="EnableSubmit3(this)" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?> 
        </label> 
       </div> 
      <?php } ?> 
      <!-- /input-group --> 
     </div> 
     <div class="box-footer"> 
      <input value="Submit" type="submit" id="submit3" name="submit" class="btn btn-primary"> 
     </div> 
      </form> 

,並在您控制器

if ($this->input->post('submit')) { 
foreach($all_id as $row_id) 
{ 
    if($this->input->post('id_overview'.$row_id)) 
    { 
      //do action where id is present 
    } 
    else 
    { 
      //do action if unchecked by getting id $row_id 
    } 
} 
} 
+0

你試過這個嗎? – Shibon

+0

我應該把腳本放在控制器中? –

1

未選中的複選框值將不會公佈,所以你必須使用jQuery和AJAX

你的複選框

<input class="id_overview" type="checkbox" name="id_overview[]" value="<?php echo $row['id_overview']; ?>"<?php foreach ($field as $wor){ ?> <?php if($row['id_overview']==$wor['id_overview']) echo "checked";?> <?php } ?> ><?php echo $row['title']; ?> 

您的jQuery

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("form#frm").submit(function(e) { // give a id to your form 
      e.preventDefault(); 
      var ids = new Array(); 
      $('.id_overview').each(function() { 
       if ($(this).is(':checked')) { 
       } else { 
        ids.push($(this).val()); 
       } 
      }); 
      $.ajax({ 
       // send ids through your ajax code 
      }); 
     }); 
    }); 
</script> 

創建一個新函數並調用它由ajax刪除ID。

+0

我不明白ajax腳本:( –

+0

使用jquery,我們正在檢查複選框被選中或沒有,如果取消選中然後推到一個數組。這個ID將傳遞給控制器​​使用ajax的新功能。 – parth

相關問題