Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda