在插入数据库时​​需要识别动态输入/文本区域 [英] need to identify dynamic input/textareas when inserting to database

查看:105
本文介绍了在插入数据库时​​需要识别动态输入/文本区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



由于每组都有多组,所以我不能逃避抓住输入数组中的所有数据。

我需要识别每个事物,因为每个输入都相互关联。



因为我没有一定数量的字段,所以我无法预测与什么相关的内容。

这里是一个快速视频,显示表单的内容将做到: http://www.screenr.com/lxe



在用户输入他们的信息后,在提交时,我将使用php来获取所有发布的数据(有点不确定我将如何做,因为我无法确定每组结果及其措施) 。

我有2个表格,结果,度量,下面你会看到我将如何将每个度量与其结果关联起来。



结果表:outcome.id,outcome.description

measure t能够:measure.id,measure.description,measure.outcome_id

任何帮助表示赞赏。

解决方案

您将需要在Javascript中手动管理数组索引。但似乎你只需要计算结果ID,所以:

  var outcome_num = 0; 

$(button.addoutcome)。click(function(){
$(form)。append(< input name ='outcome [+ outcome_num + ]'...>)
});

度量列表将包含 outcome_num

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

所以你最终得到:

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

<输入名称='结果[2]'...>
< input name ='measure [2] []'...>
< input name ='measure [2] []'...>

这样您将以可用的关联格式接收数据。只要循环 $ _ REQUEST [结果] [$ i] $ _ REQUEST [measure] [$ i] [$ x] 子数组。



如果每个度量子条目都需要添加_description字段,那么您也必须手动计数它们。在这种情况下,最好在JavaScript中复制整个数组结构以跟踪现有的痕迹。从现有的表单字段中提取索引编号的工作量更大 - 可以使用正则表达式进行处理,但需要更多的工作,所以我更喜欢这里的手动计数器。似乎在你的情况下可行,除非我错过了什么。


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.

解决方案

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.

这篇关于在插入数据库时​​需要识别动态输入/文本区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆