Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hey, how would I pass an array

Status
Not open for further replies.

rtnMichael

Programmer
Aug 5, 2002
152
US
hey,
how would I pass an array to the next page? I'm declaring and populating it through a javascript function, if that makes a difference.

Thanks
M
 


First page:
Code:
'# Loop through each value in array, and store it in a string delinated by a pipe symbol

For each StringValue in strArray

 strStoreArray = strStoreArray & StringValue & "|"

Next

'# Put in a session
Session("StoreArray") = strStoreArray

Next page

Code:
'# Split each value at the pipe symbol, creating the array again..

strSplitStoredArray = Split(Session("StoreArray"), "|")

'# then you have your array back to normal...
www.vzio.com
ASP WEB DEVELOPMENT



 
so would I have to transform my JavaScript function to VBScript for that? Or would the

var scalingArr = new Array();

declaration not make a difference when passing?
 
Wow, for some reason I didn't even realize you were doing it in JavaScript... um well Im not that proficient at javascript, Im guessing you could do it in that, but Im not sure how.

- Jason www.vzio.com
ASP WEB DEVELOPMENT



 
I don't think translating the function to VBScript will be too hard, it's a small function. I can't find any way to pass the array by itself to another page through JavaScript, though.

Thanks
M
 
I would try the built-in
Code:
toString()
function of the Array object. This returns a comma-delimeted String containing all the Array elements which should be possible to send as a parameter.
On the receiving page the Array could be initiated again using the
Code:
new Array(element0, element1, ..., elementN)
constructor, substituting the parameters with the parameter you sent from the other page.

Does it make any sense?
 
so how would that go? Would I just declare it like:
var StringArray;
StringArray = tostring(--what goes here?--);

then pass it to the next page as a hidden, then get it like a regular variable?

Am I on the right track?
 
There are also join and split functions for javascript that work similarly to the ones in ASP.

Code:
var myArray, myString;
//fill myArray with data

Using the array to string method:
myString = myArray.toString();

Using the join method:
myString = join(myArray,",")

To split the string back into an array in ASP on the next page:
Dim prevPageArray
prevPageArray = Split(Request.Form("nameOfHiddenField"),",")

If you were processing the form with GET than you would use Request.QueryString

The toString javascript method for an array will create a comma delimited string of all the values of the array. The join method allows you to specify the delimiter, if it is a large group of text entries,you may want to use join and choose a less often used character than comma as your delimiter.

Hope that helped
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
ok, so the "NameOfHiddenField" comes from where? Is it the form name?

let me just show you what I'm doing:

function check_scaling_values(max_parm)
{
var scalingArr = new Array();
var scaling_txt, scaling_txtArr;

var i = 1;
j = 0;
while (i < max_parm)
{
if (form3[&quot;scaling&quot;+i].value != 100)
{
scalingArr[j] = form3[&quot;scaling&quot;+i].value;
scaling_txt = form3[&quot;scaling_txt&quot;+i].value;
scaling_txt = scaling_txtArr.toString();
scaling_txt = join(scaling_txtArr,&quot;,&quot;)
j = j + 1;
}
i = i + 1;
}
newmax_parm = j;
}

<input type='button' name='btnSubmit' value='Submit Scalings' onclick='check_scaling_values(<%=max_parm%
>);submit();'>

What part am I missing here?
 
ok here's the updated version, now I just need to find out how to send it off to the next page. I see what you meant by the hidden field, but won't it send 'undefined' to the next page because the value wasn't set until after the function was called?

function check_scaling_values(max_parm, scalingstring)
{
var scalingArr;
var scaling_txt, scaling_txtArr;

var i = 1;
scalingArr = new Array();
j = 0;
while (i < max_parm)
{
if (form3[&quot;scaling&quot;+i].value != 100)
{
scalingArr[j] = form3[&quot;scaling&quot;+i].value;
j = j + 1;
}
i = i + 1;
}
scalingstring = scalingArr.toString();

newmax_parm = j;
}

<input type='hidden' value='<%=scalingstring%>' name='scalingstring'>

<td width=200><input type='button' name='btnSubmit' value='Submit Scalings' onclick='check_scaling_values(<%=max_parm, scalingstring%>);submit();'>

I am having trouble on the passing of the scalingstring. It just gives me errors.

I think I'm just looking at this too much, and the obvious just isn't looking too obvious anymore
 
Your setting the value of your hidden field in ASP, the ASP is running before the page gets to the client and than is done. In order to set the value in javascript:
Code:
function myFunc()
{
   frmSample.hdnParm.value = &quot;hello&quot;
}

than the form looks like this:
<form name=&quot;frmSample&quot; action=&quot;wherever.asp&quot; method=POST>
<input type=&quot;hidden&quot; name=&quot;hdnParm&quot;>
<input type=&quot;submit&quot;>
</form>

The way to set the value of a hidden input (or text, password, etc) is to reference it by formname.fieldname.value = value

So after you set scalingString you will want to set the value of your hidden field = scalingString. You may want to set it to text temporarily for testing rather than hidden so you can see the value get set.

Hope that helps
-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
ok, I get what you're saying, but what if the 'scalingstring' is set after the button is pressed, is that any different?
 
I don't mean to throw a monkey wrench in the works, but I have a site where I build an array on the initial page and then store it in a Session variable.

On subsequent pages I retrieve the array this way:
var aryChain = Session(&quot;aryChain&quot;); (that's using javascript)

Then I iterate through the array this way:

for ( key in aryChain ) {
Response.write (aryChain[key][1]); //example usage
}

Minimal code and it works just fine.
 
ok, so you build the array in, say your first page, then pass it to a session like this?:
var strArray = new Array();
session(&quot;PassingArray&quot;) = strArray;

then in the following pages, do this:

var newArray = session(&quot;PassingArray&quot;);

Now a few questions:

1. Do you populate the array in the first page and pass it through just fine to the next pages?

and

2. do you do the var aryChain = Session(&quot;aryChain&quot;); in the function or at the top of every page?

what I'm doing is this:

function check_scaling_values(max_parm)
{
form3.scalingstring.value = scalingArr;
}
but this is after the page generation. I don't get the values of the array until after the button has been pushed. (that's the way it should be)

I pass the variable to the web page before it's set with the button. The button does the function, then passes the value in 'form3.scalingstring.value'....well it should, at least. I get an error on that line. I'm not sure what I'm doing wrong here...(-:
 
1. Do you populate the array in the first page and pass it through just fine to the next pages?

Yes, I'm sending a full populated array as a session variable. The session variable itself won't act as an array (at least I haven't been able to yet) which is why I pull it out of the Session variable.

2. do you do the var aryChain = Session(&quot;aryChain&quot;); in the function or at the top of every page?

In my situation I am pulling out the array inline rather then from a function. However, it should work either way provided the scope of the variable is set properly. Meaning, don't declare you're array variable that's receiving the session variable within the function. Declare it outside and then assign it from within the function. That should keep it's scope good for the whole page.


As for the problem you're having I'm not quite clear on what you're expecting form3.scalingstring.value to look like. If it's supposed to hold just one value from the array then you need to do something like form3.scalingstring.value = scalingArr[0] - that way the code knows which value to pull out of the array.
 
so if it is a single value, seperated by &quot;,&quot; after each entry, how would I go about seperating it? I need 'form3.scalingstring.value' to be an array, then go to the next page and get each value from it. I have the values going into the array, but the next page treats it as a string and when I index it to print out, it prints out one long line.

here's where I set the variables from the previous page:
set scalingstring = request(&quot;scalingstring&quot;) 'Array from prev page'
set newmax_parm = Request.Form(&quot;newmax_parm&quot;) 'index'
set scalingstring_txt = Request.Form(&quot;scalingstring_txt&quot;) ' name of txt portion of scalingstring'


here's the write portion of it:

for i = 1 to newmax_parm - 1
TSObjNew.WriteLine(&quot;Scaling:&quot;&scalingstring_txt(i)&&quot;:&quot;&scalingstring(i))
strLineText=&quot;Scaling:&quot;&scalingstring_txt(i)&&quot;:&quot;&scalingstring(i)
intLineNum=intLineNum+1
line(intLineNum)=strLineText
next

do I need to declare it an array again in the next page?
 
The way I'm using it the string is converted to an array in the 'for' statement. So you will need to split yours up into an array.

I would do it this way in a javascript function:

function SplitToArray(parmString) {
var newArray = parmString.split(&quot;,&quot;) //split on commas
return newArray;
}

Then add these lines at the beginning of you 'for' statement:

scalingstring_txt = SplitToArray(scalingstring_txt)
scalingstring = SplitToArray(scalingstring)

I think the rest of your code should work, but I can't really test it. You might need to change the scalingstring_txt(i) to use [] instead of ().
 
sounds good to me, I think the 'split' will do the trick.

Thanks :)
M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top