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

246
Visualizações
Resetting a form with 'sticky' input values (a solution)

I searched widely to find a solution for this, and eventually had to realise (as others have before me) that a form will always reset to the values with which it is loaded.

So my solution is to load the form with empty values, and then use pure Javascript to update the values from other hidden inputs. It requires one hidden input, and one line of JS, for each user input. My example below shows the code for just one input. Any further inputs follow the same pattern.

The input (I have omitted the label for clarity):

    <li>
      <input type="text" name="firstname" id="firstname" value='' tabindex="1" size="45" />
    </li>

The hidden input:

    <input type = 'hidden' id = 'jfn' name = 'jfn' value = '<?php echo $firstname; ?>' />

The Javascript (in the 'onload' or (if jQuery is available, 'document ready') function)':

    document.getElementById('firstname').value = document.getElementById('jfn').value;

When the form is submitted the inputs (in $_POST) are saved to SESSION, whence they can be recalled for as long as the SESSION lasts:

    foreach ($_POST as $name=>$value) {
      $_SESSION['email'][$name] = $value;
    }

and later recovered:

    if (!empty($_SESSION['email'])) {
      extract ($_SESSION['email']);
    }

When/if the form is reloaded, the input values are initially empty. The previously input values are loaded into the hidden inputs. After the page has reloaded those values are transferred to the (visible) inputs. The 'Reset' button will now clear the form.

about 4 years ago · Juan Pablo Isaza
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