Updating a database using a checkbox in a table row
Updating a database using a checkbox in a table row
I have a table populated by a database. One column named Done contains all checkboxs. If the database field contains a 0 the checkbox is unchecked, a 1 would mean it is checked. I need to update the database by either checking or unchecking the the checkbox.
I am using the editable plugin on the other columns but it will not work in the column with the checkbox. I am using the following line to initialize editable: $('#itemsTable tbody td:not(td:nth-child(3))').editable('update.php', {... and it allows editable to ignore the checkbox column.
I have been searching for a way to send a 0 or a 1 to the database when the checkbox is changed. Detecting the click on the checkbox is no problem, I'm not to experienced with DataTables and can't figure out how to update the database directly from the table without using editable.
Any info is appreciated or maybe there is an example I've overlooked.
Thanks, Jim
I am using the editable plugin on the other columns but it will not work in the column with the checkbox. I am using the following line to initialize editable: $('#itemsTable tbody td:not(td:nth-child(3))').editable('update.php', {... and it allows editable to ignore the checkbox column.
I have been searching for a way to send a 0 or a 1 to the database when the checkbox is changed. Detecting the click on the checkbox is no problem, I'm not to experienced with DataTables and can't figure out how to update the database directly from the table without using editable.
Any info is appreciated or maybe there is an example I've overlooked.
Thanks, Jim
This discussion has been closed.
Replies
$('#itemsTable tbody').on('click', ':checkbox', function() {
if ($(this).attr('checked')){
// unchecked becomes checked here take CHECKED action
var row = $(this).closest('tr').get(0);
var aPos = oItemsTable.fnGetPosition(row);
var aData = oItemsTable.fnGetData(aPos);
$.ajax({
type:'POST',
url: 'update.php',
data: 'id='+aData[0]+'&col=Done&value=1'
});
} else {
// checked becomes unchecked here take UNCHECKED action
var row = $(this).closest('tr').get(0);
var aPos = oItemsTable.fnGetPosition(row);
var aData = oItemsTable.fnGetData(aPos);
$.ajax({
type:'POST',
url: 'update.php',
data: 'id='+aData[0]+'&col=Done&value=0'
});
}
});