Input forms disappear when selecting the back button.

diecasthft01 used Ask the Experts™
Good morning. I have a question related to actions using javascript and then using a back button on a submit page. I have a javascript function that allows a user to add additional input fields on a web page. So basically a user has one line that they can add an item and then if they need to add additional items, they hit a button and a new input line shows up. Once they do this up to 5 times, these items will be entered into a database with an action page. All of this works as designed. The submit page does some validating, and asks additional questions so I run the forms data to the submit page. But what I noticed is that if the user selects the back button, the added input fields do not show up on the previous page. Is there a way to make those added input forms stay visible when selecting the back button on the page?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Distinguished Expert 2018
That is one of the problems with dynamic content.

There are several ways you can get around this if you look around.

For example:

usually a page will load the data from DB on page load

I don't know how your page work but if the data is saved in DB you will need a way to force a page reload to get the newly data.

There is a simple way to do it using Javascript

Open in new window

if the data is not save then you need a way to temporary saved the data in localstorage or similar way.
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

You could use Web Storage

Based on your exact requirement, you can go with localStorage or sessionStorage, add these additional fields on the page(as well as in the web storage) when the user clicks on the back button, on the page load(you might have to use Jquery's ready method), just read those values and populate those fields as required.

Most Valuable Expert 2017
Distinguished Expert 2018

Save your dynamically added fields to local storage as they are created.
When your form load check local storage to see if anything has been saved there - if it has run the saved fields through your renderer to create them on the form.
When the form is saved to the db clear local storage.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial