How to retain form input?

I have got a form, quite long actually. After submitting it, if e. g. a typo is discovered, one has to go back to the form and input correct data. However, one will discover one has to start over because everything has disappeared. Very annoying.
Is there a method to retain what was filled in in the first place?
Lennart EricsonAsked:
Who is Participating?
Olaf DoschkeConnect With a Mentor Software DeveloperCommented:
While we're at JS/jQuery validation. When you do form submit from there as ajax call, it may report back the successful submit or a message about missing/wrong input your code then alerts the user. In this variant, you stay with the form page and thus also don't lose any state of form elements.

Then there is the variation where your form creating and processing PHP is the same script, that outputs empty values when $_POST doesn't exist and $_POST values as far as you have them, so the form is prefilled with current input, unless the submit is accepted after validation and the script either creates the next empty form or redirects to a next page. It's perhaps still easiest to implement this way.

Bye, Olaf.
Scott Fell, EE MVEConnect With a Mentor Developer & EE ModeratorCommented:
There are more than one options here. But one way is to have your form set up with the value set as a session like below.

<form action="process.php" method="post">
     <label for="frmName">Name</label>
    <input id="frmName" name="full_name" type="text" value="<?php echo $_SESSION['full_name']?>">
<button type="submit">Submit</button>

Open in new window

Then on your process.php page
$_SESSION['full_name'] = $_POST['full_name'];

Open in new window

This is of course over simplified.  You will want to make sure the session values for the form are cleared at first page load.  You will probably also want to clear the values of the session for those form items once everything is successful.

It is a good idea to have javascript/jquery validation. One good plug in is  Having client side validation will help when there are easy errors you can catch before submitting such as a field is missing or perhaps not formatted correctly. However, you still need to validate on the server side as you can't trust client side validation.
Lennart EricsonAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.