Forum Moderators: open

Message Too Old, No Replies

Translation from VB into JS

thanks

         

txbakers

1:29 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In reading a Request.Form I need to loop through the items and build a string. I'm using the ASPUpload component which doesn't handle the select multiple very well. You need to loop through the Request(Upload) object and if it's the one with multiples, then build the string for insertion into the DB.

Here is the VB code they provide, I need it in JS.

For Each Item in Upload.Form
If Item.name = "FORM_SELECT" Then
Response.Write Item.Value & "<BR>"
End If
Next

I can handle the middle, it's the outside loop I'm not sure how to write in JS.

THANKS.

Bernard Marx

2:12 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm a bit confused about the context here. I this client-side, or is it server-side JScript?

I'm assuming that we are client-side, and enumerating a form's elements.

A JS for..in loop is different to a VB for each, in that it enumerates an object's keys (as opp to values). A for..in will also spit out the 'length' key when enumerating a node list.

So enumeration via numerical index is better-suited.


var elements = Upload.form.elements;

for(var k=0, elm; elm=elements[k]; k++)
{
if(elm.name == 'FORM_SELECT')
Response.Write(elm.value + '</BR>');
}


Yet, the inclusion of 'Response.Write' suggests that this is in fact server-side, and Upload.form is part of the environment (somehow). I'd need to know more - but it wouldn't be very different.

txbakers

3:04 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Bernard - it's server side.

I've been fussing with your code, still not quite there yet. I get an error on the

for(var k=0, elm; elm=elements[k]; k++)

line with "elements[k]" throwing a null object. do we need to declare it as an array first? I wouldn't think so.

Bernard Marx

3:30 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because I was assuming client-side, where a form element has an 'elements' collection.

Upload.form doesn't have this. So what does it have?

I'll now assume that it is some form of enumerable.

It could be an object that is indexed
- by string keys
- by numerical keys
- both

It could be something darker, like a Dictionary.

Seeing as I don't know what structure is has, I'll try wrapping it in an enumerator, which will hopefully cover a number of possibilities.


var eUploadForm = new Enumerator(Upload.form);

for(var elm; elm=eUploadForm.item(); eUploadForm.moveNext())
{
if(elm.name == 'FORM_SELECT')
Response.Write( elm.value + '<BR>');
}
// If you want to do it again later
// then rewind:
eUploadForm.moveFirst();

Bernard Marx

3:53 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<edited: changed property capitalisation (may not matter)>


var eUploadForm = new Enumerator(Upload.Form);

for(var elm; elm=eUploadForm.item(); eUploadForm.moveNext())
{
if(elm.name == 'FORM_SELECT')
Response.Write( elm.Value + '<BR>');
}
// If you want to do it again later
// then rewind:
eUploadForm.moveFirst();

txbakers

4:38 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks I figured it out:

var fcnt = new Number(upload.Form.count);
for (var i = 0; i < fcnt; i++){

if (upload.Form.Items(i).Name == "selClass") classes += (upload.Form.Items(i).Item + ",");

}

I needed to use the count property to run the loop, then use the Name and item properties to check.

Bernard Marx

4:53 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah yes. There's that too.

It appears to be similar to enumeration of a

files
collection when using JScript / WSH in your PC's file system, where you can take either approach

- I add a custom getNext method to the Enumerator prototype that combines item and moveNext, so I can make the Enumerator approach meet my exacting aesthetic demands.

Out of interest, could you tell me whether or not the code I posted works too?
(I don't have ASP)

txbakers

5:04 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, it didn't work, but rather than just post that way (I hate reading those...) I just dug out the books and started from scratch to find it.

Bernard Marx

6:00 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Most disatisfying.
But thanks anyway.

..but.. that's not fair!

The name condition changed from "FORM_SELECT" to "selClass"
Did you plug that in?



Not important, but this line is odd:

var fcnt = new Number(upload.Form.count);

a) the count property is a number already.
b) new Number() produces a Number object, which then must be evaluated to a number primitive. To convert a string to a number, it's better to leave out the

new
, or just multiply by 1.

txbakers

6:22 pm on Oct 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, I changed the form field name before I tested it, yours and mine. You're right about the Number, probably don't need that in there.