POST array string manipulation

Discussion in 'PHP' started by pHrEaK, Dec 16, 2011.

  1. #1
    I was wondering if what i've got going on here was going to work or not?

    for($i=0; $i<=$specsEntered; $i++)
    {
    	$specNames[$i] = $_POST['specName' + $i];
    }
    Code (markup):
    The reason for this for loop is to loop through the amount of information a user has entered in a form. I have it set up dynamically using javascript so that whenever the user wants to add another record it creates the instance in a table. The name for each input field is specName + number, say the user clicks the "add row" button 4 times, then there would be for input fields specName1, specName2, specName3, and specName4.

    What's needing to happen is for the php page that actually pulls the info and queries the database to be able to know how many variables to create to store information to depending on $specsEntered, which is the number of times user clicked the button(amount of input fields)....

    Does it look like what i have going on above would work? If not any ideas?

    thanks,
     
    pHrEaK, Dec 16, 2011 IP
  2. luckyguy354

    luckyguy354 Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I suggest you use this
    for($i=0; $i<$specsEntered; $i++)
    {
    	$specNames[$i] = $_POST['specName'][$i];
    }
    Code (markup):
    And on the form, you can use:

    
    <input type="text" name="specName[0]" />
    <input type="text" name="specName[1]" />
    .....
    
    Code (markup):
     
    luckyguy354, Dec 16, 2011 IP
  3. pHrEaK

    pHrEaK Active Member

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Works like a charm! Thanks friend!
     
    pHrEaK, Dec 16, 2011 IP