-1
var add_dynastfileds = function()
{
    CheckedFileds = [];
    UncheckedFileds = [];

    $(".DynFileds").each(function() {
        //console.log($(this).val());
        if ($(this).is(":checked")) {
            if(CheckedFileds.indexOf($(this).val())==-1){
                    CheckedFileds.push($(this).val());                  
                }
        } else {
                if(UncheckedFileds.indexOf($(this).val())==-1){
                    UncheckedFileds.push($(this).val());                  
                }
        }
    });
    $.post("<?=base_url()?>asset/addDynFields/",{CheckedFileds:CheckedFileds,UncheckedFileds:UncheckedFileds},function(result)
    {
        //console.log(result);
        $.gritter.add({
                        title: "Fields updated successfully ",
                        text: "",
                        time: 2000
        });
    });

}
<div class="panel-group">
    <div class="panel panel-default">
        <div class="row">
<?
    foreach($dynast_fields as $dynfileds){
        if(($dynfileds['display_name'] == 'Title') && 
            ($dynfileds['add_to_listing'] =='1'))
        {
            $styleClass = 'disabled="disabled"';
            $checked = "checked";
        }else{
            $styleClass = '';
            $checked = "";
        }
?>
                    <div class="col-md-1"></div>
                    <input type="checkbox" <?=$styleClass?> <?=$styleClass?> class="DynFileds" name="dynfileds<?=$dynfileds['rec_no']?>" id="dynfileds<?=$dynfileds['rec_no']?>" value="<?=$dynfileds['rec_no']?>" <? if($dynfileds['add_to_listing'] == '1') { echo "checked"; } ?>/>
                    <span style="font-size:12px;font-family: Open Sans,Helvetica Neue,Helvetica,Arial,sans-serif;"><?=$dynfileds['display_name']?></span>
                    <br/>
<?php 
    }
?>
        </div>
    </div>
    <span id='dup_checkdiv' style="display:none;" class="text-danger"></span>
    <br/>
    <div class="popover-footer" style="align:center;">
        <div class="row">
            <div class="col-md-3"></div>
            <div class="col-md-3">
                <a href="javascript:;" style="font-family: Open Sans,Helvetica Neue,Helvetica,Arial,sans-serif;" class="btn btn-info btn-sm" onclick="add_dynastfileds()">Save</a>
            </div>
        </div>
    </div>
</div>

public function addDynFields()
{
    $checkedfileds = $_POST['CheckedFileds'];
    $uncheckedFileds = $_POST['UncheckedFileds'];

    $this->db->update('pm1asset_dynamic_fields',array('add_to_listing' =>0));

    if(!empty($checkedfileds))
    {
        for($i = 0; $i < count($checkedfileds); $i++)
        {
            $data = array(
            'add_to_listing' =>1
            );
            $this->db->set($data);

            $this->db->where('rec_no',$checkedfileds[$i]);
            $this->db->update('pm1asset_dynamic_fields');
        }
    } 
}

Here I have written some code to update the value of checked and unchecked checkbox, for the first time it was working fine by uploading the value '0' to unchecked and the value '1' to checked checkbox, but after the page is reloaded, if i am going to update the value '0' by unchecked the previous checked checkbox, it's not going to update because after page is refreshed the checkbox value remains checked. This problem occurs due to i have give the condition in the checkbox of div tag, but i need this condition also. Is there any way to write it differently. how to resolve this problem can any one please tell me.

Deeksha
  • 3
  • 4
  • Have you checked if the post data contains the correct ids and the field is an array? where_in expects the values to be an array (or a single value). – D B Apr 09 '20 at 06:44
  • Thanku for your reply, i have checked by giving alert, for checked checkbox it give's correct value in array but for unchecked it's giving whole checkbox value. i don't why – Deeksha Apr 09 '20 at 06:49
  • Can any one please help me to resolve this problem.. – Deeksha Apr 10 '20 at 03:20

1 Answers1

0

Try something like this

$('.DynFileds:checked').each(function() {
   CheckedFileds.push(this.value);
});
$('.DynFileds:not(:checked)').each(function() {
   UncheckedFileds.push(this.value);
});

More

Dum
  • 1,431
  • 2
  • 9
  • 23