1

I have a form, which allows the user to add multiple 'row' of the same input fields, basically, asking users to specify the number of children they have.

The form is very simple, as follows (abbreviated for reading purposes):

<form name="guestform" method="post">
   ....
   <div class="children-part">
      <ul>
         <li>&nbsp;<li>
         <li>
            <label>Name:</label>
            <input type="text" name="childfirstname" />
         <li>
         <li>
            <label>&nbsp;</label>
            <input type="text" name="childlastname" />
         <li>
         <li>
            <label>Gender:</label>
            <input type="radio" name="childgender" value="M" /> <label>Male</label>
            <input type="radio" name="childgender" value="F" /> <label>Female</label>
         <li>
      </ul>
   </div>
   ....
</form>

I have a [+] button on the page that, via JavaScript, clones the div.children-part block for an n-th amount of entries (usually up to 3).

I take the form and serialize the data so I can pass this via an AJAX call to a web service to be inserted into the database. Now, for a single child it's easy to get the query string values, but it becomes more complicated for more than one...

example of a complex query_string:

&childfirstname=1&childlastname=1&childfirstname=2&childlastname=2&childfirstname=3&childlastname=3&childfirstname=4&childlastname=4

My question is, is there a way to get these values into an array without writing complex loop statements?

I currently split them on the $-character, into an array and then determine how many of a single string literal this array contains, then iterate over each one individually to build up a multi-dimensional array of the data that I need (more than one child), but this feels like overkill for something so simple?

JadedEric
  • 1,943
  • 2
  • 26
  • 49
  • 1
    Have you tried adding `[]` to the `name`? Then you should be able to access them via `$_GET`. But beware, the keys could get mixed up, so it might make sense to add them when creating the query – kero Aug 18 '14 at 21:43
  • 2
    I'm not sure if it's what you're looking for, but you can name form fields with arrays like this: ``. Each field will post as an array: `foreach ($_POST['childfirstname'] as $key => $value) { ... }`. – showdev Aug 18 '14 at 21:43
  • Of course :$... I'll try that quickly. Thanks for the heads up on the mixing up part, will keep an eye out. Thanks – JadedEric Aug 18 '14 at 21:45
  • In the query string `childlastname=1&childlastname=2` the value of the single resulting variable `$_POST['childlastname']` would be `2` since the first occurrence in the query string is overwritten. – feeela Aug 18 '14 at 21:53
  • related question: http://stackoverflow.com/questions/6152436/posting-array-from-form – feeela Aug 18 '14 at 21:56

1 Answers1

4

Adding [] should allow you to iterate and access the data as parallel arrays. An alternative would be adding and maintaining an index for each "child" and incrementing that. e.g. name="childfirstname[currentIndex]" Each time a row is added, you update and increment a javascript variable containing the current index.

<form name="guestform" method="post">
   ....
   <div class="children-part">
      <ul>
         <li>&nbsp;<li>
         <li>
            <label>Name:</label>
            <input type="text" name="childfirstname[]" />
         <li>
         <li>
            <label>&nbsp;</label>
            <input type="text" name="childlastname[]" />
         <li>
         <li>
            <label>Gender:</label>
            <input type="radio" name="childgender[]" value="M" /> <label>Male</label>
            <input type="radio" name="childgender[]" value="F" /> <label>Female</label>
         <li>
      </ul>
   </div>
   ....
</form>
Tim S
  • 5,023
  • 1
  • 34
  • 34