• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 686
  • Last Modified:

JavaScript: preserve fields after refresh

below is a javascript function which dynamically appends some fields to a form.
Using a submit button:
<input type = "submit" value = "More on this date" name = "more" onClick="AddText();">

i want to preserve these added fields if the page refreshed after clicking this button. Is this possible?
I really need your help. thanks.

     var fieldCount = 2;  #the first two fields
     var fieldCount1 = 2;

    function addSku() {


       var node=document.createTextNode("GEAR (Enter SKU): ");

       var newFriend = document.createElement('input');
       newFriend.type = 'text';
       newFriend.size = '17';
       newFriend.value = '';
       newFriend.onFocus = this.value="";
       newFriend.name = 'sku' + fieldCount;
       newFriend.id = 'sku' + fieldCount;

       var node=document.createTextNode(" ");

       var newFriend1 = document.createElement('input');
       newFriend1.type = 'checkbox';
       newFriend1.name = 'gear' + fieldCount1;
       newFriend1.id = 'gear' + fieldCount1;
   var node=document.createTextNode(" Review?");

var newFriend2 = document.createElement('br');

3 Solutions
if you are submitting on the same page, just put your function on the onload event.

<body onload="addSku()">
ZvonkoSystems architectCommented:
No, it is not possible. The HTTP protocol is a stateless protocol. Therefore is the browser loading into the window that what is sent for the URL. There is no state before and after the page.
So if you want to repeat some user actions then you need some onLoad function which will look for some cookies that are not cleared on page form onSubmit action and try to analyze what you have to recreate on the page. But that is definitely not a behavior and design that I would recommend to you.

Instead of using a submit button why dont you just use a normal button to fire the event. You could then use javascript to dynamically create the new rows without leaving the page

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

As noted above, HTTP is stateless.  So, in order to have this information "preserverd", you could/should save information into cookies.
If someone fills out an form and did not add all info or something was incorrect, you want to display back to the what they typed  -  is that correct?????  

(reload the form with original info intact?)

Do you have PHP avail?
JaimeJegoniaAuthor Commented:
thanks to everyone who answered
JaimeJegoniaAuthor Commented:
i used a button instead, so definitely the page would not be refreshed. It has an "onclick" attribute which calls a function in javascript to retrieve the data from the fields. javascript-perl connection is done through ajax.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now