I have a checkbox when checked on it i want to enter the value of the checkbox into the database.Please help me to find the Ajax and php code for this. I tried this
$(document).on('click', "input[type='checkbox']", function() {
var checkbox = $(this);
var checked = checkbox.attr('checked');
$.ajax({
url:"<?php echo base_url("contact-details-availability"); ?>",
type: 'post',
data: {
action: 'checkbox-select',
id: checkbox.attr('contact_avl'),
checked: checked
},
success: function(data) {
//alert(data);
},
error: function(data) {
// alert(data);
// Revert
checkbox.attr('checked', !checked);
}
});
});
and
public function add_contact_details_availability()
{
if($_POST['action'] == 'checkbox-select') {
$checkbox = $_POST['id'];
$checked = $_POST['checked'];
// Your MySQL code here
echo 'Updated';
}
echo 'Code ran';
}
But it won't works
But it won't workswhich part wont work here??var_dump($_POST);you ll see what all values have posted.changeevent in place ofclick, one more thing replacecheckedto any other because there is one attribute with same name. Let me know.