2016-06-09 64 views
-1

被髮送到數據庫作爲「0」這是我的看法如何設置複選框的值,如果該複選框未選中的值將在笨

<div class="checkbox checkbox-info checkbox-inline"> 
       <input type="checkbox" name="box" id="busy" <?=($restaurant_info["restaurant_busy"] != 0)? "checked":""?>/> 
       <label for="busy">Busy</label> 
      </div> 
      <br><br> 
       <div class="row"> 
        <div class="col-md-6"> 
         <select id="dropdownHolder" name="restaurant_busy"> 
          <option value="1" <?php if ($restaurant_info["restaurant_busy"] == '1') echo 'selected = "selected"'; ?>>30 minute</option> 
          <option value="2" <?php if ($restaurant_info["restaurant_busy"] == '2') echo 'selected = "selected"'; ?>>60 minute</option> 
          <option value="3" <?php if ($restaurant_info["restaurant_busy"] == '3') echo 'selected = "selected"'; ?>>90 minute</option> 
          <option value="4" <?php if ($restaurant_info["restaurant_busy"] == '4') echo 'selected = "selected"'; ?>>120 minute</option> 
         </select> 
        </div> 
       </div> 

這是我的jquery:

$(function() { 
    var selected = $('#dropdownHolder option:selected'), // Seems to be unused 
     $busy = $('#busy'), // Always cache your queries 
     $dropdown = $('#dropdownHolder'); // Caching queries 

    $dropdown.hide(); // Hidden by default initially 

    $busy.change(function() { 
     if ($busy.prop('checked')) { 
      $dropdown.show().focus().click(); 
     } else{ 
      $busy.prop('checked', false); 
      $dropdown.blur().hide(); 
     } 
    }); 

    $busy.change(); // This sets initial state 

});

$("#edit_restaurant").submit(function() { 
    $("#edit_restaurant").attr('action', '/VENDOR/Vendor/change_restaurant/'); 
    this.submit(); 
}); 

這裏是我的更新

function change_restaurant(){ 

    if(!isset($_COOKIE["vendor_login"])){ redirect("/VENDOR",'refresh'); } 
    if(!$this->Token_m->m_check_token($this->input->cookie('vendor_login'),$this->input->cookie('vendor_token'))){ 
     setcookie('vendor_login', '', time() - 3600, '/'); 
     redirect('/VENDOR/',"refresh"); 
    } 
    if ($this->input->cookie('vendor_login') != null) { 
     $admin_name = $this->input->cookie('vendor_login'); 

     setcookie('vendor_login', $admin_name, time() + 28800, '/'); 
     $msg = $this->input->cookie('vendor_token'); 
     setcookie('vendor_token', $msg, time() + 28800, '/'); 
    } 

    if($vendorname = $_COOKIE["vendor_login"]) { 

     $check_login = $this->Vendor_m->m_get_user_by_vendor($vendorname); 
     $restaurant_id = $check_login["restaurant_id"]; 
     { 

      if ($_POST == NULL){ 
       redirect("/VENDOR/Vendor/vendor_setting","refresh"); 
      } 
      $data = array(
       "restaurant_busy" =>$this->input->post("restaurant_busy"), 
       "restaurant_active" =>$this->input->post("restaurant_active"), 
       "delivery_active" =>$this->input->post("delivery_active"), 
       "takeaway_active" =>$this->input->post("takeaway_active"), 
       "voucher_active" =>$this->input->post("voucher_active"), 

      ); 
      $this->Vendor_m->m_update_restaurant_info($data,$restaurant_id); 
     } 
    }redirect("/VENDOR/Vendor/vendor_setting","refresh"); 
} 

控制器,這是我的模型

功能m_update_restaurant_info($數據,$ restaurant_id) {

$this->db->where("restaurant_id", $restaurant_id); 
    $this->db->update("uhd_restaurant", $data); 
} 

值1, 2,3和4來自下拉菜單,並帶有數據庫表fie ld的名字是「restaurant_busy」,我想如果複選框沒有被選中,將會更新爲「0」值到restaurant_busy,我可以幫助我嗎?

+0

你能告訴我怎麼做嗎?,我認爲這是我的回答什麼即時通訊試圖找到@moped –

回答

1

我們將添加一個隱藏的輸入並根據從下拉列表中選擇的選項更新其值(select元素),同時我們將從select中移除name="restaurant_busy"並將其移至隱藏輸入以便提交值,以便您的html將這個樣子......

<div class="checkbox checkbox-info checkbox-inline"> 
    <input type="checkbox" name="box" id="busy" <?=($restaurant_info["restaurant_busy"] != 0)? "checked":""?>/> 
    <label for="busy">Busy</label> 
</div> 
<br><br> 
<div class="row"> 
    <div class="col-md-6"> 
     <input type="hidden" id="restaurantBusyInput" name="restaurant_busy" value="<?=$restaurant_info["restaurant_busy"]?>"> 
     <select id="dropdownHolder"> 
      <option value="1" <?php if ($restaurant_info["restaurant_busy"] == '1') echo 'selected = "selected"'; ?>>30 minute</option> 
      <option value="2" <?php if ($restaurant_info["restaurant_busy"] == '2') echo 'selected = "selected"'; ?>>60 minute</option> 
      <option value="3" <?php if ($restaurant_info["restaurant_busy"] == '3') echo 'selected = "selected"'; ?>>90 minute</option> 
      <option value="4" <?php if ($restaurant_info["restaurant_busy"] == '4') echo 'selected = "selected"'; ?>>120 minute</option> 
     </select> 
    </div> 
</div> 

這將是你的JS

$(function() { 
    var selected = $('#dropdownHolder option:selected'), // Seems to be unused 
     $busy = $('#busy'), // Always cache your queries 
     $dropdown = $('#dropdownHolder'), // Caching queries 
     $hiddenInput = $('#restaurantBusyInput'); 

    $dropdown.hide(); // Hidden by default initially 

    $busy.change(function() { 
     if ($busy.prop('checked')) { 
      $dropdown.show().focus().click(); 
     } else{ 
      $hiddenInput.val('0'); 
      $dropdown.blur().hide(); 
     } 
    }); 
    $dropdown.change(function() { 
     if ($dropdown.val()) { 
      $hiddenInput.val($dropdown.val()) 
     } else { 
      $hiddenInput.val('0') 
     } 
    }); 

    $busy.change(); // This sets initial state 
}); 

在這裏,我們我們隱藏的輸入設置爲0,如果選擇沒有價值或複選框取消選中。讓我知道它是怎麼回事;)

+0

它仍然無法正常工作,當我試圖取消選中複選框,比我提交我沒有得到的價值,比提交的價值仍然在上次提交 –