Looping through textfields

Discussion in 'PHP' started by lost, Nov 22, 2005.

  1. #1
    I have an indefinite number of textfields created on my page. I need to loop through each one and retrieve the data that was input by the user and stor them so that i can load them into the database.

    The textfields are created in a loop that are named incrementally, description textfield and a part no textfield, as such:

    desTextfield1 pnoTextfield1
    desTextfield2 pnoTextfield2
    desTextfield3 pnoTextfield3
    desTextfield4 pnoTextfield4
    desTextfield5 pnoTextfield5
    desTextfield6 pnoTextfield6
    .
    .
    .
    desTextfieldn pnoTextfieldn
    Is this code correct...in that the textfields name is referred to as textfield1, textfield2, etc..?

    
         document.getElementById("textfields").innerHTML += 'Item '+numItems+'   Description<INPUT TYPE="text" NAME="idescription '+numItems+'" >
    Part Number <INPUT TYPE="text" NAME="ipartno '+numItems+'" VALUE=""  ONKEYDOWN="if (event.keyCode==9) { addNewItem(); }">';
    
    Code (markup):
    If so, how do i go about looping through those textfields? I was thinking that maybe i will be needing a 2 dimensional array...that way each element of the array will hold a description field and a part no field. Then when i need to load into the database i could loop through each array element and populate the database.

    Please let me know if this would work correctly and how i would go about it.

    Thanks in advance.
     
    lost, Nov 22, 2005 IP
  2. bob_sepc

    bob_sepc Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You want to name the text fields as an array:

    text_field[]

    Then you can loop through them in your processing code.

    I hope this helps,

    Bob
     
    bob_sepc, Nov 23, 2005 IP
  3. hdpinn

    hdpinn Peon

    Messages:
    48
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Description<INPUT TYPE="text" NAME="idescription[]" >
    Part Number <INPUT TYPE="text" NAME="ipartno[]" VALUE="" ONKEYDOWN="if (event.keyCode==9) { addNewItem(); }">';
     
    hdpinn, Nov 26, 2005 IP