Search code examples
javascripthtmldjangocaching

Cache form data against page refresh


I have certain forms in my Django project and I want to get a different data with a button on this form with a pop-up window and save it to the database. But here, how can I make the previously entered data come back when the page is reloaded so that my background data is not lost?

Thanks in advance to everyone who helps.


Solution

  • LocalStorage has a setItem method. You can use it like this:

    var inputEmail= document.getElementById("email");
    localStorage.setItem("email", inputEmail.value);
    

    When you want to get the value, you can do the following:

    var storedValue = localStorage.getItem("email");
    

    It is also possible to store the values on button click, like:

    <button onclick="store()" type="button">StoreEmail</button>
    
    <script  type="text/javascript">
      function store(){
         var inputEmail= document.getElementById("email");
         localStorage.setItem("email", inputEmail.value);
        }
    </script>
    

    To use cookie instead, you can refer to this article.