Ann
Ann

Reputation: 471

How to insert data into db using checkbox using ajax and php without form

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

Upvotes: 2

Views: 6727

Answers (2)

Suyog
Suyog

Reputation: 2482

Here is the working JQuery

<script>
        $(document).on("change", "input[name='chk']", function () {
            var checkbox = $(this);
            var checked = checkbox.prop('checked');
            $.ajax({
                url:"<?php echo base_url("contact-details-availability"); ?>",
                type: 'post',
                data: {
                    action: 'checkbox-select', 
                    id: checkbox.data('contact_avl'), 
                    checked: checked
                      },
                success: function(data) {
                    //alert(data);
                },
                error: function(data) {
                   // alert(data);
                    // Revert
                    checkbox.attr('checked', !checked);
                }
            });
        });
    </script>

HTML

<input type="checkbox" name="chk" id="chk" data-contact_avl="val" value="1">check

I am saying it as working because all the three values are passing. Changes done to fix are:

1] changed var checked = checkbox.attr('checked'); to var checked = checkbox.prop('checked');

2] changed id: checkbox.attr('contact_avl') to id: checkbox.data('contact_avl')

3] changed onclick to onchange

Upvotes: 2

Nishanth Matha
Nishanth Matha

Reputation: 6081

try this:

var id = checkbox.attr('contact_avl'),
$.ajax({
        url:"<?php echo base_url("contact-details-availability"); ?>",
        type: 'post',
        data: {
            action: 'checkbox-select', 
            id: id, 
            checked: 'checked'
              },
        success: function(data) {
            //alert(data);
        },
        error: function(data) {
           // alert(data);
            // Revert
            checkbox.attr('checked', !checked);
        }
    });

Upvotes: 0

Related Questions