0

first off all, i want to thank for this site, it helped me alot. So, I want to do a button/switch that when it is pressed it passes a value "1" to another page and when it's not pressed (default) it passes the value "0". I already have a code that i made with the help of user of this site and youtube:

 <script type="text/javascript">

    function onoffalarm(){
    var onof = document.getElementById('onof');
    var switchonoff = document.getElementById('switchonoff');
    if(onof.style.display=="block")
    {
        onof.style.display= "none";
        switchonoff.src= "images/buttonOFF.png";
        switchonoff.title= "Turn On";
        window.location.href="led.php?value=0";
    }else{
        onof.style.display="block";
        switchonoff.src= "images/buttonON2.png";
        switchonoff.title= "Turn OFF";
        window.location.href="led.php?value=1";
    }        
}

<img id="switchonoff" onmousedown= "onoffalarm()" src="images/ButtonOFF.png" width="217" height="217" alt="" title="Turn On">

<div id="onof">
</div>

With this code, the only value that passes to "led.php" is "1", but as i said, i want it to pass the value 1 when pressed and 0 when it is not pressed. Adicionaly, I also want to know how can i save the button state when i make a refresh for exemple. How do i do that.

Seen Already

https://www.youtube.com/watch?v=A-3BxXdISG8

How to get JS variable to retain value after page refresh?

How to get javascript variable value in php

Thanks for the help

Community
  • 1
  • 1
d_Sotero
  • 23
  • 1
  • 5
  • @synquall maybe it very useful information, but im a noob, so i dont know who to use the code that you gave me. the code that i made (that doesnt work well xD) take me alot of time because im new at this. And about the other problem that only sends 1 to the page led.php you can help with the solution. keep in mind that im very bad at this. thanks for the help. – d_Sotero May 18 '14 at 00:21

1 Answers1

0

You could use cookies in JavaScript to store variables which are available browser-wide. These variables are not lost when you refresh the page or close the page. It will only be deleted if the user clears their cookies or the cookies expire.

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime()+(exdays*24*60*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

setCookie("state", 0, 30);

setCookie(nameOfCookie, valueOfCookie, expiryInDays)

And you can fetch them too.

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++)  {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) 
            return c.substring(name.length,c.length);
    }
    return "";
}

var cookieState = getCookie("state");

getCookie(nameOfCookie)

Hope it helped.