Dynamic forms and PHP
<p>Ive started working on a dynamic form script that allows a user to add form elements via Jquery, which is then in turn submitted to a PHP script.</p>

<p>I'm just after some feedback on ways to achieve this. At the moment I have the following:</p>

<p>When a user adds a form element the element is added with the following name array:</p>

<pre><code>&lt;textarea name="element[text][123]"&gt;
&lt;input type="text" name="element[input][456]" /&gt;
</code></pre>

<p>As I need to know the type of form element that was submitted I am using a multidimensional array called 'element[][]' where the first level of the array is the type of element and the second element of the array is a unique ID and the value.</p>

<p>When I var_dump() This after submission PHP outputs:</p>

<pre><code>array
text =&gt; array
123 =&gt; string 'The textarea value'

input =&gt; array
456 =&gt; string 'The input field value'
</code></pre>

<p>Im working on the PHP side of the script now and just wondering if there is a better way to do this. </p>

<p>Any thoughts?</p>

<p>UPDATE</p>

<p>I have to change the way that Im doing this as the array keynames are not unique.
If the user adds two textareas</p>

<pre><code>&lt;textarea name="element[text][123]"&gt;
&lt;textarea name="element[text][456]"&gt;
</code></pre>

<p>When the user adds a form element, the element can be dragged so the positioning can be changed after the element was created. This allows a user to add an element but then move it to where they want it to appear.</p>

<p>PHP handles this ordering fine and accepts the array in the order that the form is submitted, however as mentioned above if the key names are the same then the order will be broken.</p>

<p>On the PHP side I need to know</p>

<ol>
<li>the type of form field</li>
<li>the value of the form field</li>
<li>the unique ID, which is just a timestamp, of the form field</li>
</ol>

<p>I think I might need to do what Cole mentioned, assigning the names as:</p>

<pre><code>element[text_123]
</code></pre>

<p>I can then explode the keyname on '_' to determine the type and the identifier.</p>

<p>UPDATE</p>

<p>I took the script Jack posted and slightly modified it</p>

<pre><code>$vars = $_POST['element'];

foreach ($vars as $id =&gt; $vals)
    {
    // $vars[id] outputs the ID number
    // $vars[vals] is the array containing the type and value
    echo "This fields ID is $id. ";
    foreach($vals as $key =&gt; $value)
        {
        echo "Type was: $key and the value was: $value &lt;br /&gt;";
        }
    }
</code></pre>

<p>A quick test of this outputted</p>

<pre><code>This fields ID is 1338261825063. Type was: heading and the value was: xzczxczxczxczxczxc
This fields ID is 1338261822312. Type was: heading and the value was: asdasdasdasdad 
</code></pre>

<p>From this I know the identifier and the array that it belongs to, the type and the value, but I also know the order that the data was submitted.</p>

<p>From that I can wrap my data in markup, perform any additional operations and then insert the data into the database.</p>
