Kelvin Halim Kelvin Halim - 5 months ago 15
HTML Question

how to set value of checkbox, if the checkbox is not checked the value will be send to database as "0" in codeigniter

this is my view

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


this is my 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();
});


here is my controller for update

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


and this is my model

function m_update_restaurant_info($data, $restaurant_id)
{

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


value 1, 2, 3 and 4 is from the the dropdown menu, and with database table field name is "restaurant_busy", and i want if the checkbox is not checked, will be update as "0" value into restaurant_busy, i can u help me?

Answer

We'll add a hidden input and update it's value based on option selected from dropdown ( select element ), also we will remove name="restaurant_busy" from select and move it to hidden input so that it's value is submitted, so your html will look like this...

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

This would be your 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
} );

Here we set our hidden input to 0 if select has no value or checkbox is unchecked. Lemme know how it goes ;)