1

What's the easiest way to re-populate a form when the page is reloaded? Is there a script in jquery, php, or javascript/ajax that would achieve this?

If the submitted failed, I want the user to go back. My problem is all my CSS hovers and background-changes don't work via keyup.

Erik
  • 5,701
  • 27
  • 70
  • 119

1 Answers1

3

If you mean reloaded before it is submitted you will need to use JavaScript to capture and store the values in a cookie, or to the server using Ajax, and then refill the form on page load.

If you mean after it is submitted then will need to use PHP to re-populate the form with the submitted form values:

<input type="text" name="somefield" value="<?php if (isset($_POST['somefield'])) echo htmlspecialchars($_POST['somefield'], ENT_HTML5, 'utf-8'); ?>">
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 2
    +1 but I would recommend changing it to `htmlspecialchars($_POST['somefield'])` so that user values don't break the form / html. – jeroen Apr 25 '12 at 01:40
  • 1
    Good point. I guess if I went so far as to check if it was set before echoing it I should take this all the way – John Conde Apr 25 '12 at 01:40
  • @JohnConde Jeroens suggestion is correct, this code is insecure. Use ENT_QUOTES or ENT_COMPAT (or omit it!). See also http://stackoverflow.com/a/14532168/427545 for a discussion why `ENT_HTML5` is useless with htmlspecialchars. – Lekensteyn Jan 26 '13 at 00:19