Input forms disappear when selecting the back button.

diecasthft01
diecasthft01 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

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

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

For example:
http://yaragalla.blogspot.com/2013/10/preserving-dynamically-added-content.html
Hi,

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
location.reload();

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

Commented:
You could use Web Storage https://www.w3schools.com/html/html5_webstorage.asp

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.

Regards,
Chinmay.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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