Click to See Complete Forum and Search --> : storing and repopulating options


LAwebTek
01-24-2003, 12:04 PM
Well, I posted my project and got no comments, so maybe it was information overkill. I really, really need a solution though so I'm putting it in hypathetical terms so maybe someone can figure a good solution. All I need is to be able to save and reload the list via a cookie. Ok, here's the sitch:

<form name="BigList">

<input type="text" name="Ftext">
//user inputs text here and it creates the option.text
//of an option in document.BigList.List below

<input type="text" name="Fval">
//user inputs text here and it creates the option.value
//of an option in document.BigList.List below

<input type="button" value="Add" onClick="AddNewOption()">
//I already have a function for this button.

<select name="List"></select>
//this is the "list" with options created from above text fields

<input type="button" value="Save" onClick="saveFunction()">
//this is the function I need.
<input type="button" value="Load" onClick="loadFunction()">
//I need this one too.

</form>

The entire code that I am working with is posted in another thread entitled "list options via cookieArray" which might be useful. I am asking the question again in this thread because I thought maybe someone would have a better idea of how to do this if they didnt look at my original code lol.

Thanks in advance if you spend time on this, my brain hurts after a week of trying without success. If you solve this one for me, I just might go and name my next born after you!
(or at least my next cat lol )

Cheers.

LAwebTek
01-24-2003, 03:23 PM
The problem is that I can't "hard code" the options into arrays to set in the cookies because they don't exist until the user adds files to the list and presses "Save". I have written a function already to try to accomplish this (it is present in my other post) but it returns "TheList0=undefined" as the information stored on the cookie instead of the desired array. So the question, I guess, is how do I properly create the cookieArray so that it stores the desired values? Once I've accomplished that I think I can get the function that loads the values to work on my own.

LAwebTek
01-24-2003, 03:46 PM
In the original code I am using functions called setCookieArray and getCookieArray to store an array on a cookie like this..

function setCookieArray(name){
this.length = setCookieArray.arguments.length - 1;
for (var i = 0; i < this.length; i++) {
this[i + 1] = setCookieArray.arguments[i + 1]
setCookie (name + i, this[i + 1], expdate);
}
}


var expdate = new Date();
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000 * 365));

function storeVal() {
storedVal = new Array;
storedVal[1] = document.Aclock.Ahour.value;
storedVal[2] = document.Aclock.Amin.value;
storedVal[3] = document.Aclock.APM.value;
storedVal[4] = document.Sdelay.delay.value;

var testArray = new setCookieArray("Settings", // cookie name
storedVal[1], //array 2 - Alarm Clock Hour
storedVal[2], //array 3 - Alarm Clock Minutes
storedVal[3], //array 4 - Alarm Clock AM or PM
storedVal[4] //array5 - Snooze Delay Time
);

var testArray = new getCookieArray("Settings");
}

so what I can't seem to do is to plug in the select options since I can't define them as above. I will try doing it the way you recommended, however if you, or anyone else, knows how to incorporate the existing code above please let me know.

Thanks tremendously for your help.
Sorry I can't pay you, but I'm working for free too. When this is finished I plan to release it publically as freeware. I will give you credit inside the code and in the "about" page for your contributions though!

LAwebTek
01-24-2003, 03:58 PM
Sure you can. Check out the entire app

http://www.freecashjackpot.com/javaAMP/javaAMP.html

I use the above functions to save the alarm settings and it works fine, check out the source :)

LAwebTek
01-25-2003, 05:57 PM
ok - thanks to Dave's help with strings, objects, and cookies the saveList function now works. I tried to write the function to repopulate the select options, but I'm obviously doing something wrong at the end of the code. In my test I save 3 items to the list and when it repopulates it does create 3 options, however only the first option contains the proper text and value and the other 2 display undefined. Here's the code:

function saveList() {
var newList = document.amp.playlist.options;
var j, len = newList.length;
var Sngstr = "";
for (j = 0; j < len; j++) {
if (Sngstr.length > 0) Sngstr += "&";
Sngstr += newList[j].value + "|" + newList[j].text;
}
setCookie ("TheList",Sngstr,expdate);
}

function loadList() {
var i, j;
playList = document.amp.playlist.options;
TheList = getCookie("TheList");
if (TheList != null) {
listArray = TheList.split("&");
for (i = 0; i < listArray.length; i++) {
j = i + 1;
piecesArray = listArray[i].split("|");
playList.text = piecesArray[j];
var no = new Option();
no.value = piecesArray[i];
no.text = piecesArray[j];
playList[playList.length] = no;
}
}
}

Any ideas?

LAwebTek
01-25-2003, 06:03 PM
I just realized that this line

playList.text = piecesArray[j];

was not doing anything and should be removed.
The code still doesn't work though.

LAwebTek
01-25-2003, 08:37 PM
hey I was close. lol

Thanks Dave! - I think that you are too modest about your skills on your web page.

cheers.