Click to See Complete Forum and Search --> : How to store an array to a hidden fld


smde
01-21-2003, 01:18 PM
I have developed a web page which allows the user to select
numerous items from a combo - box. Each selection is stored in an array. I allow the user then to review the selected items - adding or deleting as needed until all the selections are correct. All the items are elements of an array.
When the form submit button is selected I am trying to concatenate all the array elements to one string and assign the string to a hidden field which I have already defined on the form.
I have tried this and tried this and I cannot tell why it is not working ...
the hidden field always appears blank

I am using the following function to concatenate the array elements and assign the resulting string to the hidden field named selected_forms:

function arrange(){
var xobj= 1;
var formStr;

for(xobj>0;xobj<IDSelect;xobj++){
if(formsel[xobj]!= ""){
formStr=formStr+formsel[xobj];
}
}
document.CopyCenter.selected_forms.value="Line="+formStr;
}

This doesn't work - an ASP script which is used to process the form - data . reports this field as blank or that the data is undefined ????????
Help please - Thank you .

Dan Drillich
01-21-2003, 02:34 PM
I think you need to initialize the variable formStr. When I don’t initialize formStr in the code below, the string holds the NaN value.


<html><head><title>Getting and using a window reference</title>
<script language="JavaScript">
<!-- hide me

var xobj = 0;
// Please initialize the variable formStr
var formStr = "";


for(xobj>0;xobj<5;xobj++){
formStr = formStr + xobj;
}


var x = formStr;


// show me -->
</script>
</head>
<body>

<script language="JavaScript">
document.write(x);
</script>

</body>
</html>



Cheers,
Dan

smde
01-21-2003, 03:12 PM
Thanks for the input - I found out that in my case the problem was really that I needed to declare the variable globally.
In the function I was using, the scope of the variable was local and the data did not exist at the time that the form was being submitted bercause the function had already run and the data object was destroyed .
By declaring the variable formStr at the top of my routine and outside of any functions it automatically becomes a global variable and is visible to everything including the post routine....

Thank you for responding - in the example that I supplied - you were right about the data needing something initialized -
warm regards and thanks again ;


Michael