I have a form that has a button that allows form fields to be dynamically added. The form itself saves data fine, but when I add rows, the data is saved fine EXCEPT for checkbox data. If I add the rows, save the form, then reopen and apply checkboxes, it works though.
Here is my code :
//adds extra table rows
var i=$('table tr').length;
$(".addmore").on('click',function(){
addNewRow();
});
$(document).on('keypress', ".addNewRow", function(e){
var keyCode = e.which ? e.which : e.keyCode;
if(keyCode == 9 ) addNewRow();
});
var addNewRow = function(){
html = '<tr>';
html += '<td><input type="checkbox" name="data[InvoiceDetail][0][added]" id="added_1'+i+'" class="form-control autocomplete_txt" autocomplete="off"></td>';
html = '</tr>';
$('table').append(html);
$('#caseNo_'+i).focus();
i++;
}
//to check all checkboxes
$(document).on('change','#check_all',function(){
$('input[class=case]:checkbox').prop("checked", $(this).is(':checked'));
});
Table data pertaining to form :
<?php if(isset($invoice['InvoiceDetail'])&&!empty($invoice['InvoiceDetail'])){?>
<?php foreach ( $invoice['InvoiceDetail'] as $key=>$item){?>
<tr id="tr_<?php echo $key+1?>">
<td> <input class="case" type="checkbox"/> </td>
<td class="prod_c">
<td><input type="checkbox" <?php if(isset($item['added']) && $item['added'] == 1) echo "checked=\"checked\""?> data-type="checkbox" name="data[InvoiceDetail][<?php echo $key;?>][added]" id="added_<?php echo $key+1?>" class="form-control autocomplete_txt" autocomplete="off"></td>
</tr>
Button to call event :
<div class='row'>
<div>
<button class="btn btn-danger delete" type="button">- Delete</button>
<button class="btn btn-success addmore" type="button">+ Add More</button>
<button class="btn btn-success copy" type="button">Copy Selected</button>
</div>
</div>
Working Code :
html += '<td><input type="checkbox" name="data[InvoiceDetail]['+i+'][added]" id="added_1'+i+'" class="form-control autocomplete_txt" autocomplete="off"></td>';