Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Store form in localstorage, retrieve it later for submit

In a wordpress website I want that when someone fills a product form (select attributes etc) and clicks buy now, if is not logged in then he is redirected to login form. On succesfull login I want to get the submitted form contents from localstorage, fill the form and then submit the form automatically.

Is that possible?

I found that I can store the submitted form like this (but how I can refill the form automatically?):

$(document).ready(function()    {  
    $('form').submit(function(e) {    
        e.preventDefault();
        var formFields = $(this).serialize();
        localStorage.setItem('myForm', formFields);    
        //data = localStorage.getItem('myForm');  //retrieve it later
     });
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should be able to retrieve data from localStorage, get your form fields using DOM selectors and fill their values using the data from localStorage.

In vanilla JS, you can probably do the following:

document.addEventListener('load', () => {
  // check if user is logged in according to your usecase
  if(isLoggedIn()) {
    try {
      const formData = JSON.parse(localStorage.getItem('myForm'));
      // get input fields. If you have some unique id associated to them, you can use document.querySelector(`#${inputId}`);
      const inputFields = document.querySelectorAll('input');
      for(let i = 0; i<inputFields.length; i++) {
         const inputId = inputFields[i].getAttribute('data-id');
         // fallback to empty string in case it is not found in localStorage object
         inputFields[i].value = formData[inputId] || '';
      }
    } catch(e) {
      // handle error
    }

    // Now you have the form input fields pre-filled, add custom logic from here .. 
  }

})

/* 
  --- Sample DOM ----
  <input data-id="name" />
  <input data-id="city" />


  ---- Form fields saved in localStorage ---
  { name : 'Naruto', city: 'Leaf Village' }
*/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!