在插入数据库时​​需要识别动态输入/ textareas

问题描述:

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.

我有一个表单,允许用户动态添加额外的输入和textareas。 p>

由于每个都有多组,我无法逃脱只是抓住输入数组中发布的所有数据。 p>

我需要识别每一个东西,因为每个 输入相互关联。 p>

由于我没有一定数量的字段,我无法预料到与什么有关。 p>

这里是 一个快速视频,显示表单的用途: http://www.screenr.com/lxe p>

用户输入信息后,在提交时,我将使用php获取所有发布的数据(有点不确定我将如何做到这一点,因为我无法识别每组结果和 它的措施)。 p>

我有2个表,结果,测量,下面你会看到我将如何将每个指标与其结果联系起来。 p>

结果表:outcome.id,outcome.description p>

度量表:measure.id,m easure.description,measure.outcome_id p>

感谢任何帮助。 p> div>

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;

$("button.addoutcome").click(function(){
     $("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.

I would suggest naming them into arrays

example might be something like this

    <input name="outcome[i]" type="text" />
    <input name="outcome[i][measure][]" type="text" />

where i is a number you define when you add elements

then in your php you can parse them in for loop or whatever