lundi 6 février 2017

checkbox value resets on page reload using local storage

I was working on a module which lets user compare different products,every time user select on a checkbox associated with it, its got appended to compare box,its url get updated for e.g.: compare.php?id=23,43,23,and on next page parse these id's to get the result corresponding to those id's, now I'm stuck on a problem when user navigate on multiple pages using pagination or uses Ajax filter to filter result, whole checkbox url get reset to earlier url i.e compare.php.
Now all I want is to hold the selected checkboxes value on page refresh, i know I can do it via

          if(isset($_GET['category']))
                  {if(in_array($category_row['course_category_name'],$_GET['category']))
                    {
                      $category_check='checked="checked"';
                    }
                    else
                    {
                      $category_check="";
                    }
                  }

But I don't want to use this approach, problem with this approach is, i want to store those checkbox for longer time or for multiple browsing sessions.
After lots of googling I found out it can be achieved via browser's local storage.

So guys help me to keep checkbox value checked on page reload using local storage.

My code is : HTML

 <div class="col-md-12 col-sm-12 col-xs-12" style="margin: 0px">
 <div class="checkbox-inline col-md-12 col-sm-12 col-xs-12"><label><input type="checkbox" class="compare-result" id="compare-<?php echo $course_id;?>" value="<?php echo $course_id;?>">Compare</label></div>




Aucun commentaire:

Enregistrer un commentaire