0

I have a vote counter with a button that will get disabled once the user has clicked on it. However, when the page is getting refreshed, the button will become available again. How do i make the button remain disabled even after the user refreshed/closed the page?

this is my current code https://hastebin.com/oyolabefal.xml

  • If I am not wrong, you are expecting the user's click should be persisted. So, on reload you can disable the button. You can use local storage or cookie based on your business needs. – Sathibabu Oct 22 '17 at 09:07
  • I think you should have a table to store if user has vote before or not on it. Local storage will get empty after refreshing/closing web page. –  Oct 22 '17 at 09:07
  • 1
    Please put the code into the question itself in the form of a [mcve]. – LW001 Oct 22 '17 at 09:26

3 Answers3

1

You can use local storage. But it will still limit to one browser. For eg, if user voted from chrome and try your page in Firefox, it will not work. Also, it will not work if user clear the storage.

You can set a key and read that to disable that particular button via JS.

If you want to control this feature across browser, you might need to save this info in DB

Rohit Verma
  • 299
  • 1
  • 8
0

The local storage is only on aktuell Divice. When you check localStorage, when it has value you must prevent votting

<html>
<head>
<script language="javascript">
function clickCounter() {
    if(typeof(Storage) !== "undefined") {
        if (!localStorage.clickcount) {
             localStorage.clickcount = 1;
        }else{
            document.getElementById("voted").innerHTML ="You have allreay Voted"; //show message you have allreay Voted
        }
        document.getElementById("result").innerHTML = localStorage.clickcount;
    } else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
    }
}
    </script>
<style>
    .counter {
        position: absolute;
        border: 1.5px solid #50915F;
        width: 350px;
        height: 200px;
        background: white;
        top: 30%;
        left: 40%;

        }

    .keke{
        position: absolute;
        border-width: 0;
        background: transparent;
        top: 30%;
        left: 44%;
        text-align: center;
        margin: auto;
        font-size: 45px;
        }
    .gbutton {
        position: absolute;
        top: 60%;
        left:37%;
        }
            .button {
        background-color: #50915F;
        border: none;
        color: black;
        padding: 15px 32px;
        text-align: center;
        text-decoration: none;
        display: inline-block;
        font-size: 16px;
        margin: 4px 2px;
        cursor: pointer;
        -webkit-transition-duration: 0.4s;
        transition-duration: 0.4s;
    }

    .button2:hover {
        box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);
    }
</style>
</head>
<body>
    <div class="counter">
        <center><font face="Lato" id="voted"> Vote here</font></center><br>
        <div id="result" class="keke"></div>
        <button  class="button button2 gbutton" onclick="clickCounter();this.disabled=true" type="button">Vote</button>
    </div>
</body>
</html>
ferhado
  • 2,363
  • 2
  • 12
  • 35
0

yes localstorage is one of the best option to do the task, on the server side you can save the status in db and show the button conditionally.

for example

if x.present?
<button disabled > click </button>
else
 <button> click </button>
end

hope that helps

Apoorv
  • 1,338
  • 1
  • 17
  • 18