vendredi 4 mai 2018

My cookie function does not work (php)

I have a cookie function that is supposed to remember the boxes that I ticked however when I refresh the page, checked boxes are unchecked.I don't know how to fixes this problems.

My code :

<script type="text/javascript">
function setCookie(c_name,value,expiredays) {
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate)
}

function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1) { 
            c_start=c_start + c_name.length+1 
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
                return unescape(document.cookie.substring(c_start,c_end))
        } 
    }
    return null
}
onload=function(){
document.getElementById('linksNewWindow').checked = getCookie('linksNewWindow')==1? true : false;
}
function set_check(){
setCookie('linksNewWindow',  document.getElementById('linksNewWindow').checked? 1 : 0, 100);
}
</script>
<label  >device selection :</label> 
<?php
for($i=0;$i<count($sensors_name_perso);$i++):
?>

 <div class="checkbox">
            <label >

            <input type="checkbox"  data-cookie-checkbox-id="linksNewWindow" data-cookie-checkbox="true" data-cookie-checkbox-key="sensors_name_perso"  data-cookie-checkbox-value="sensors_name_perso<?php echo $sensors_name_perso[$i]; ?>" data-cookie-checkbox-name="sensorsensors_name_perso[]" <?php if(isset($_GET['sensors_name_perso']) && in_array('sensors_name_perso'.$sensors_name_perso[$i], $_GET['sensors_name_perso'])){echo ' checked="checked"';} ?> onchange="set_check();" /> <?php echo $sensors_name_perso[$i]; ?> 


            </label>
            </div>
            <?php
            endfor;
            ?>




Aucun commentaire:

Enregistrer un commentaire