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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call a specific control from a control array

Status
Not open for further replies.

Phailak

Programmer
Apr 10, 2001
142
0
0
CA
Hail,

Let's say I have three text boxes in my submit form that I name the same to put in an array:

<input name='txtInput[]' size='5'>
<input name='txtInput[]' size='5'>
<input name='txtInput[]' size='5'>

If I want to know the value of textbox # 2, how would I do that?
I thought:

msgbox txtInput(2).value

But obviously that doesn't work... the reason I'm doing it this way, is that I nee the array for PHP where as once submitted I can retrieve the values from each text box

Thanks

Phailak
 
IE doesn't offer "control arrays" though it does provide an easy to use implied element collection facility.

I think this is what you want:
Code:
<HTML>
<!-- saved from url=(0013)about:internet -->
  <HEAD>
    <SCRIPT language="VBScript">
      Option Explicit

      Sub cmdEchoBack_onclick()
        Dim intCollIndex
        Dim strEcho

        With frmInput
          For intCollIndex = 0 To .txtInput.length - 1
            strEcho = strEcho & .txtInput(intCollIndex).value & vbNewLine
          Next
        End With
        MsgBox strEcho
      End Sub

      Sub window_onload()
        frmInput.txtInput(0).focus
      End Sub
    </SCRIPT>
  </HEAD>
  <BODY>
    <FORM name="InputForm" id="frmInput">
      <INPUT type="text" name="txtInput" id="txtInput" size="5">
      <INPUT type="text" name="txtInput" id="txtInput" size="5">
      <INPUT type="text" name="txtInput" id="txtInput" size="5">
      <INPUT type="button" id="cmdEchoBack" value="Echo Back">
    </FORM>
  </BODY>
</HTML>

Note the use of the elements' id attribute, which in general should be made the same as an element's name in those cases where you really want to use name. Typically name is simply a tag used to relay form elements sent back to the server. But there is no rule saying they have to have the same value (see the <FORM> tag in my example).

When you do not explicitly assign id to an HTML element, IE will try to coerce it to be the same as the name when one has been assigned, but this can fail. Therefore always explicitly assign id to any HTML element you want to reference in script.

Now these are implied collections, implied by identical ids on elements within a namespace scope (such as a given form). They are unlike arrays in at least two important ways:

They have indices from 1 to {collection}.length instead of starting at index = 0, and it isn't possible to create an implied collection with only one element.

To illustrate the latter case, if you had only one of those <INPUT id="txtInput"> elements you'd get a script error because you do not have a collection with one member, you just have an element.

To test this delete or comment two of them out of my example code above. The page will fail in the window_onload() event handler because there is no collection to index in such a case.
 
Thanks, problem is it's not working the way I want it too, maybe it's the way I'm doing it?
See the way I named the text boxes <input name='txtInput[]' size='5'>, I'm using the [] for array purposes in PHP when submitting a form. This [] is what seems to be causing the problem, now I cannot refer to the text boxes anymore in vbscript???
 
As I said, blindly using name (which is only intended for binding form values in a request, i.e. "submit" action) can cause lots of woes.

IE will attempt to create a matching id value where it can. However you have chosen to use characters in your name values that are illegal in an id.

Running the test code below shows that IE gives up and fails to assign an id based on your names:
Code:
<HTML>
<!-- saved from url=(0013)about:internet -->
  <HEAD>
    <SCRIPT language="VBScript">
      Option Explicit

      Sub cmdListIDs_onclick()
        Dim intCollIndex
        Dim strList

        With frmInput
          For intCollIndex = 0 To .children.length - 1
            strList = strList _
                    & """" & .children(intCollIndex).id & """" _
                    & "<BR>"
          Next
        End With
        lblIDs.innerHTML = strList
      End Sub

      Sub window_onload()
        frmInput.children(0).focus
      End Sub
    </SCRIPT>
  </HEAD>
  <BODY>
    <FORM name="InputForm" id="frmInput">
      <INPUT type="text" name="txtInput[]" size="5">
      <INPUT type="text" name="txtInput[]" size="5">
      <INPUT type="text" name="txtInput[]" size="5">
    </FORM>
    <INPUT type="button" id="cmdListIDs" value="List IDs">
    <P id="lblIDs"></P>
  </BODY>
</HTML>

Both VBScript and JScript manipulate HTML elements by id, never by name. No ids, no manipulation!

Why on Earth PHP wants those brackets defies explanation by me, I'm no PHP guy. I'd say lose them though, most likely PHP is truncating them anyway.

If you find you do need the brackets in the names (probability near zero from where I sit) you can always manually assign usable id values just as I did in my earlier example.
 
These are my comments, maybe of some help.
[1] id and name
w3c.org standard on naming name & id:
w3c said:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

I understand you are influenced by php-specific convention carry-through, but as far as client-side is concerned, using square brackets in the name is not w3c-standard compliant and can cause trouble. However, in many many situation, you can get away without any unwelcome side-effect.

[2] You can certainly retrieve the multiple element's value with the same name client-side, server-side would be a natural thing. As dilettante well expanded, use id would be easier in this regard client-side, but certainly do-able without being too much complication. I would show you in this demo.
[tt]
<html>
<head>
<script language="vbscript">
sub showit
const sname="txtInput[]"
dim a() : redim a(-1)
dim ainput, i
dim info : info=""
set ainput=document.getElementsByTagName("input")
for i=0 to ainput.length-1
if lcase(ainput(i).name)=lcase(sname) then
redim preserve a(ubound(a)+1)
set a(ubound(a))=ainput(i)
end if
next
for i=0 to ubound(a)
info=info & "[" & i & "]" & vbtab & a(i).value & vbcrlf
next
msgbox info
end sub
</script>
</head>
<body>
<form>
<input name='txtInput[]' size='5'><br />
<input name='txtInput[]' size='5'><br />
<input name='txtInput[]' size='5'><br />
</form>
<button onclick="showit">show "txtInput[]" (sic) values</button>
</body>
</html>
[/tt]
 
Further notes:

I'd used cross-browser compliant methods, and avoided using ie proprietary document.all collection. But if vbscript is used client-side, there is no reason to feel unease using it.

 
Thanks a lot guys, I'll give it a try, I have already found an alternative using PHP code, but if this work I'd prefer this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top