Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to properly save form data in local storage for later usage in javascript

I need to save form data (firstname and lastname) in localstorage once the form is submitted so that i will not have to type it again the next time i want to submit the form.

I have tried the code below but when i refreshed the page, form data is not saved on localstorage.

<!DOCTYPE html>
<html>
  <head>
    <title></title>

  </head>
  <body>


<script  type="text/javascript">
  function store(){

     var firstname= document.getElementById('firstname').value;
     localStorage.setItem("firstname_data", firstname);

     var lastname= document.getElementById('lastname').value;
     localStorage.setItem("lastname_data", lastname);


var storedValue_firstname = localStorage.getItem("firstname_data");
var storedValue_lastname = localStorage.getItem("lastname_data");

 alert(storedValue_firstname);
alert(storedValue_lastname);

              //document.getElementById('firstname').value = storedValue_firstname;
//document.getElementById('lastname').value = storedValue_lastname;


//window.location='/';
    }
</script>
 
<form  action="test.php" class="form1"  method="post" /> 
<input name="firstname" type="text" id="firstname" class='' placeholder="firstname" /><br>
<input name="lastname" type="text" id="lastname" class='' placeholder="lastname" /><br>



<button onclick="store()" type="button">Store Form Data</button>
</form>
  </body>
</html>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can try to use windows.onload event. When you reload the page, with this event you can check if anything is stored in the localstorage or not. if stored then will set this value to the input field as required. see the example code here

<!DOCTYPE html>
<html>
  <head>
    <title></title>

  </head>
  <body>     
<form  action="test.php" class="form1"  method="post" /> 
<input name="firstname" type="text" id="firstname" class='' placeholder="firstname" /><br>
<input name="lastname" type="text" id="lastname" class='' placeholder="lastname" /><br>

<button onclick="store()" type="button">Store Form Data</button>
</form>
  <script  type="text/javascript">
  function store(){
     var firstname= document.getElementById('firstname').value;
     localStorage.setItem("firstname_data", firstname);

     var lastname= document.getElementById('lastname').value;
     localStorage.setItem("lastname_data", lastname);


      alert(storedValue_firstname);
      alert(storedValue_lastname);


    }
    
   window.onload = function exampleFunction() {
    var storedValue_firstname = localStorage.getItem("firstname_data");
    var storedValue_lastname = localStorage.getItem("lastname_data");
    if(storedValue_firstname) {
      document.getElementById('firstname').value = storedValue_firstname;
    }
       if(storedValue_lastname) {
      document.getElementById('lastname').value = storedValue_lastname;
    }
  }
</script>
  </body>
</html>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading