Solved

Array in forms

Posted on 2002-04-11
9
19,718 Views
Last Modified: 2011-08-18
I have a form like this

<form>
<input type="hidden" name="hiddenField" value="">
<input type="text" name="field[0]" value="a">
<input type="text" name="field[1]" value="b">
<input type="text" name="field[2]" value="c">
</form>

With Javascript, is there an other way to refer to "field[0]" than with

document.forms[0].elements[1].value

??

If I have a lot of form-fields in the form, it's hard to keep track of the index of each element.

What I'm thinking of is something similar to

document.forms[0].field[0].value

but which is legal.

Batalf

0
Comment
Question by:Batalf
  • 6
  • 3
9 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 6935343
I would not use [ or ] in your field names, but you can do this:

  <form name='myform'>
   <input type="text" name="field0" value="a">
   <input type="text" name="field1" value="b">
   <input type="text" name="field2" value="c">
  </form>



and then in your script:

  for ( var i=0; i<3; i++ )
  {
     alert( eval("document.myform.field" + i + ".value") );
  }
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6935347
or:

   for ( var i=0; i<3; i++ )
   {
      alert( eval("document.forms[0].field" + i + ".value") );
   }
0
 
LVL 32

Author Comment

by:Batalf
ID: 6935352
But then it's a little bit harder to read by our serverside-script(PHP). It's easier to just loop through an array.

Batalf
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 33

Expert Comment

by:knightEknight
ID: 6935360
then just give all your fields the same name:

 <form name='myform'>
  <input type="text" name="myfield" value="a">
  <input type="text" name="myfield" value="b">
  <input type="text" name="myfield" value="c">
 </form>


  for ( var i=0,n=document.forms[0].myfield.length; i<n; i++ )
  {
     alert(document.forms[0].myfield[i].value);
  }
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6935369
... the browser will create an array of fields off the form call "myfield", and you can loop thru it as above.
0
 
LVL 32

Author Comment

by:Batalf
ID: 6935378
Hm. Interesting idea you present here. I didn't know that Javascript did that. But: The problem with this solution is that PHP on the server doesn't recognize myfield as an array. It looses all values except the last one only the value "c" seems to be sent to the server.

Batalf
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6935481
well, I am not comfortable using [ or ] in field names, but if it works for you in every other respect, then perhaps a variation on my previous idea will work (not tested)


  <form>
   <input type="hidden" name="hiddenField" value="">
   <input type="text" name="field[0]" value="a">
   <input type="text" name="field[1]" value="b">
   <input type="text" name="field[2]" value="c">
  </form>



  for ( var i=0; i<3; i++ )
  {
     alert( eval("document.forms[0].field[" + i + "].value") );
  }
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 50 total points
ID: 6935486
or maybe:

 for ( var i=0; i<3; i++ )
 {
    alert( eval("document.forms[0].elements['field[" + i + "]'].value") );
 }
0
 
LVL 32

Author Comment

by:Batalf
ID: 6935491
Thank you! That did the trick.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question