2

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

5
  • 3
    But it won't works which part wont work here?? Commented Oct 7, 2015 at 4:38
  • Have you looked at firbug to check if sending to url? Commented Oct 7, 2015 at 4:39
  • the value doesn't pass and showing error undefined index id and checked Commented Oct 7, 2015 at 4:40
  • in ajax, var_dump($_POST); you ll see what all values have posted. Commented Oct 7, 2015 at 4:43
  • 1
    Try change event in place of click, one more thing replace checked to any other because there is one attribute with same name. Let me know. Commented Oct 7, 2015 at 4:45

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

i have one more doubt once we checked the checkbox it remains checked after refreshing the page.How it possible??
Most of the time its because browser's behavior. Read mode about it on this link: stackoverflow.com/questions/3176155/… and get solution for this on following link: stackoverflow.com/questions/10954839/…
0

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);
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.