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> <li>
<li>
<label>Name:</label>
<input type="text" name="childfirstname" />
<li>
<li>
<label> </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?