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?
index.php
Lennart EricsonAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & 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>
</form>

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 https://jqueryvalidation.org/  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.
1
Olaf DoschkeSoftware 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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Lennart EricsonAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.