php - How to update the value of checked and unchecked checkbox to table using codeigniter

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.
Answer
Solution:
Try something like this
$('.DynFileds:checked').each(function() {
CheckedFileds.push(this.value);
});
$('.DynFileds:not(:checked)').each(function() {
UncheckedFileds.push(this.value);
});
More
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: using $this when not in object context
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.