Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Array in forms

Posted on 2002-04-11
9
Medium Priority
?
19,744 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

722 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