-1

I have one checkbox on my form working with the database for active(1) and inactive(0) users to access via admin panel. All is working fine but the problem is its not keeping the record for the checboxes when the checkbox is checked or unchecked after refreshing/reloading the page. i want them to be remembered even after admin logout and login again. can any one help me out ? Thanx in advance

Here is HTML :

<td><input type='checkbox' class='user_status' 
       id='user_status_<?php echo $data['user_reg_id']; ?>' value='' name = 'user_status' 
                                onclick="userId(<?php echo $data['user_reg_id']; ?>)" />

Here is JS :

<script>
function userId(user_reg_id){
     $(document).ready(function(){
        var link =  "<?php echo base_url();?>" ;
            var st = $('#user_status_'+user_reg_id).is(":checked") ? 0 : 1; 
            $.post(link + "administration/um/user_status", {user_reg_id:user_reg_id, user_reg_status:st,  ajax : 1}, function(data){
                      alert (st); //showing status 1 and 0 on alert
  if (st == 1){
                          $("#user_status_").prop(":checked", true)
                             } else {
                          $("#user_status_").prop(":checked", false)
                     }; 
            });
    }); 
}
</script>
H45H
  • 1,019
  • 10
  • 28

2 Answers2

2

store checkbox value in database using ajax when you click on it.. also fetch from database checkbox value if checkbox value is 1 than checked ...otherwise uncheck :)

virendra
  • 163
  • 9
  • i have done that sir but its not working for me. i set a alert box to check whether status is giving or not and its showing the status 1 and 0 which is saving in db. i applied check if(st == 1) { $(#user_status_).prop('checked', true); } else { $(#user_status_).prop('checked', true); } but its not working. Can't figure it out why! – H45H Nov 19 '14 at 04:50
  • var st = $('#user_status_'+user_reg_id).is(":checked") ? 1 : 0; – virendra Nov 19 '14 at 11:14
  • also give condition into input box like this : if($data['checkbox value']==1) {?> – virendra Nov 19 '14 at 11:19
0

Since you need the data to be persistent, I would suggest using HTML5 localStorage. Depending on how persistent, you may need to save it in a database. I haven't tested this code and have not actually done this. It's meant to get you close to a working solution.

function isChecked(user_reg_id){
    $(document).ready(function(){ 
        $('#user_status_'+user_reg_id).on('click', function () {
            if(localStorage && localStorage.getItem(user_reg_id)){
                localStorage.removeItem(user_reg_id);
            }else{
                $('#user_status_'+user_reg_id).prop('checked', true);
            }
        }
    });
}
EternalHour
  • 8,308
  • 6
  • 38
  • 57