Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving checkbox state on reload

how do i save checkbox state through sessions? i'm using this jquery code to toggle the options:

$('div.check input:checkbox').bind('change',function(){
    $('#'+this.id+'txt').toggle($(this).is(':checked'));
});

but when i reload, the checkboxes are back to their default state. i know i'd have to use sessions, but how do i make the checkbox state persist using sessions in php?

html:

<div class="check">
<p><input type="checkbox" value="Name" id="name" checked /> <label for="name">Name</label></p>  
<p><input type="checkbox" value="Reference " id="reference" checked /> <label for="reference">Reference</label></p>
    </div>

    <div id="nametxt"> Show Name TEXT </div>
    <div id="reftxt"> Show Ref TEXT </div>
like image 756
input Avatar asked Jul 22 '10 21:07

input


People also ask

Why does the checkbox stay checked when reloading the page?

It means that the checkbox should be empty based on what you are trying to do. However if you were to click the box and reload the page your checked will be true however your input will not be checked. So the issue lies in how you are setting your DOM element to be in a checked state.

How to keep checkbox checked even after page refresh in php?

In the case of a "checkbox", you have to actually modify the <input> to include the keyword "checked". Similarly for "options" and "text". For "text" you fill in the "value=" after suitably escaping the text (using htmlentities() ).


1 Answers

Purely in JavaScript supporting localStorage if available, otherwise using document.cookie.

function getStorage(key_prefix) {
    // this function will return us an object with a "set" and "get" method
    // using either localStorage if available, or defaulting to document.cookie
    if (window.localStorage) {
      // use localStorage:
      return {
        set: function(id, data) {
          localStorage.setItem(key_prefix+id, data);
        },
        get: function(id) {
          return localStorage.getItem(key_prefix+id);
        }
      };
    } else {
      // use document.cookie:
      return {
         set: function(id, data) {
           document.cookie = key_prefix+id+'='+encodeURIComponent(data);
         },
         get: function(id, data) {
           var cookies = document.cookie, parsed = {};
           cookies.replace(/([^=]+)=([^;]*);?\s*/g, function(whole, key, value) {
              parsed[key] = decodeURIComponent(value);
           });
           return parsed[key_prefix+id];
         }
       };
     }
  }

jQuery(function($) {
  // a key prefix is used for the cookie/storage
  var storedData = getStorage('com_mysite_checkboxes_'); 

  $('div.check input:checkbox').bind('change',function(){
    $('#'+this.id+'txt').toggle($(this).is(':checked'));
    // save the data on change
    storedData.set(this.id, $(this).is(':checked')?'checked':'not');
  }).each(function() {
    // on load, set the value to what we read from storage:
    var val = storedData.get(this.id);
    if (val == 'checked') $(this).attr('checked', 'checked');
    if (val == 'not') $(this).removeAttr('checked');
    if (val) $(this).trigger('change');
  });

});

jsFiddle demo available -- Click some checkboxes, then "Run" the script again!

like image 93
gnarf Avatar answered Sep 30 '22 14:09

gnarf