handling multiple input boxes in PHP when you don't know how many there are

They have: 426 posts

Joined: Feb 2005

So I have a form with some input boxes or sets of input boxes.

I have a button that creates more sets of input boxes.

For example: one set of input boxes compromises the below:

<div class="input" id="input1" style="display:block">
                                <h4>Creative 1</h4>
                                  <h5>Width</h5>
                                  <input class="clonedwidth1" type="text" name="clonedwidth1" />
                                  <h5>Height</h5>
                                  <input class="clonedheight1" type="text" name="clonedheight1" />
                                  <h5>Creative</h5>
                                  <textarea class="clonedCreative1" name="clonedCreative1"></textarea>
                                </div>

As you can see, the name for the next set of input boxes would increment the number appended to the end of the character by one. So for clonedheight1 the next set would read clonedheight2.

How can I process all the input boxes when I dont know how many there are?

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

You can give them array names, like clonedheight[1], clonedheight[2], etc, and then loop through the post data like a regular array.

Assuming those names, the server-side processing would be something like:

<?php
foreach ($_POST['clonedheight'] as $field => $value)
{
   
//$field = the index number
    //$value = the data in the value="" field
}
?>

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.