如何设置复选框的值,如果未选中该复选框,则该值将作为" 0"发送到数据库。在codeigniter

时间:2016-06-09 04:52:10

标签: html codeigniter checkbox

这是我的观点

<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");
}

这是我的模特

function m_update_restaurant_info($ data,$ restaurant_id)     {

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

值1,2,3和4来自下拉菜单,并且数据库表字段名称为&#34; restaurant_busy&#34;,我想如果未选中该复选框,则将更新为&# 34; 0&#34;值得进入restaurant_busy,我可以帮助你吗?

1 个答案:

答案 0 :(得分:1)

我们会添加一个隐藏的输入并根据从下拉列表中选择的选项更新它的值(选择元素),我们也会从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
} );

如果select没有值或取消选中复选框,我们将隐藏输入设置为0。 Lemme知道它是怎么回事;)