I have a form that allows the user to dynamically add extra inputs and textareas.

Since there will be multiple sets of each, I can not get away with just grabbing all the data posted in the array of inputs.

I need to identify each thing, because each input relates to each other.

Since I do not have a set number of fields, I can not anticipate what relates to what.

here is a quick video showing what the form will do: http://www.screenr.com/lxe

After the user enters their info, on submit, I will use php to grab all the data posted (a bit unsure how I will do that, because I am unable to identify each set of outcome and its measures).

I have 2 tables, outcome, measure, below you'll see how I will relate each measure to its outcome.

outcome table: outcome.id, outcome.description

measure table: measure.id, measure.description, measure.outcome_id

Any help is appreciated.

Accepted Answer

You will need to manually manage your array indexes in Javascript. But it seems you only need to count the outcome ids, so:

var outcome_num = 0;

     $("form").append("<input name='outcome[" + outcome_num + "]' ...>")

And the measure list would carry the outcome_num alike, but be additonally an array itself:

.append("<input name='measure[" + outcome_num + "][]' ...>");

So you end up with:

 <input name='outcome[1]' ...>
     <input name='measure[1][]' ...>
     <input name='measure[1][]' ...>
     <input name='measure[1][]' ...>

 <input name='outcome[2]' ...>
     <input name='measure[2][]' ...>
     <input name='measure[2][]' ...>

This way you'll receive the data in a usable associative format. Just loop over $_REQUEST["outcome"][$i] and the $_REQUEST["measure"][$i][$x] subarray.

If each measure subentry needs additonal _description fields, then you will have to manually count them too. In that case it would be best to replicate the whole array structure in Javascript to keep track of existing indicies. It's more work to extract the index number from existing form fields - doable with regular expressions, but more work, so I would prefer the manual counters here. Seems workable in your case, unless I'm missing something.

Written by mario
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki