为表单创建用户定义的字段

时间:2021-03-16 12:57:22

So we have a form that has multiple fields and our employees can select/deselect each field when creating the form so that only certain fields actually appear on the form when the user views them. Sometimes, our employees would like to include a field that is not in this list. What is the best way of implementing this functionality? Should I set aside a few columns as "custom" fields and have a new table that keeps track of additions of new fields and just join the two tables when I create the form? Would this be achievable in php/mysql or is it better to use a scripting language like python/ruby?

因此,我们有一个包含多个字段的表单,我们的员工可以在创建表单时选择/取消选择每个字段,以便在用户查看表单时,只有某些字段实际显示在表单上。有时,我们的员工希望包含不在此列表中的字段。实现此功能的最佳方法是什么?我应该留出几个列作为“自定义”字段,并有一个新表跟踪新字段的添加,并在创建表单时加入两个表?这可以在php / mysql中实现,还是使用像python / ruby​​这样的脚本语言更好?

1 个解决方案

#1


0  

You can do this with php.

你可以用PHP做到这一点。

in your form:

在你的形式:

<input name="form[]" type="text" />

In your php:

在你的PHP中:

foreach($_POST['form'] as $formInput) { ... }

foreach($ _ POST ['form'] as $ formInput){...}

and in your database -- well, that's up to you, but I would do an x - y - z schema:

并且在你的数据库中 - 嗯,这取决于你,但我会做一个x-y-z架构:

where x is the user's ID, or the foreign-key - y is what the input is supposed to be (email, addr, etc) -- and z is the actual value.

其中x是用户的ID,或者外键 - y是输入应该是什么(电子邮件,地址等) - 而z是实际值。

The only problem with that would be --- let's say you have required inputs a,b,&c --- and optional inputs d,e,&f --

唯一的问题是---假设你需要输入a,b和c ---以及可选输入d,e和f -

if d&e are not required to input f, then the 'y' value of 'f' could be 4,5, or 6 -- so, it gets a little trickier in the database side, depending on what exactly you're doing...

如果d&e不需要输入f,则'f'的'y'值可能是4,5或6 - 因此,在数据库方面它会变得有点棘手,具体取决于你正在做什么。 ..

Alternatively, if you had JavaScript or jQuery adding the inputs to your form, then you could give them a name, and in your php, modify your query accordingly--- and just leave the columns blank if there was no value set for them.

或者,如果您有JavaScript或jQuery将输入添加到表单中,那么您可以给它们一个名称,并在您的php中相应地修改您的查询 - 如果没有为它们设置值,则将列留空。

#1


0  

You can do this with php.

你可以用PHP做到这一点。

in your form:

在你的形式:

<input name="form[]" type="text" />

In your php:

在你的PHP中:

foreach($_POST['form'] as $formInput) { ... }

foreach($ _ POST ['form'] as $ formInput){...}

and in your database -- well, that's up to you, but I would do an x - y - z schema:

并且在你的数据库中 - 嗯,这取决于你,但我会做一个x-y-z架构:

where x is the user's ID, or the foreign-key - y is what the input is supposed to be (email, addr, etc) -- and z is the actual value.

其中x是用户的ID,或者外键 - y是输入应该是什么(电子邮件,地址等) - 而z是实际值。

The only problem with that would be --- let's say you have required inputs a,b,&c --- and optional inputs d,e,&f --

唯一的问题是---假设你需要输入a,b和c ---以及可选输入d,e和f -

if d&e are not required to input f, then the 'y' value of 'f' could be 4,5, or 6 -- so, it gets a little trickier in the database side, depending on what exactly you're doing...

如果d&e不需要输入f,则'f'的'y'值可能是4,5或6 - 因此,在数据库方面它会变得有点棘手,具体取决于你正在做什么。 ..

Alternatively, if you had JavaScript or jQuery adding the inputs to your form, then you could give them a name, and in your php, modify your query accordingly--- and just leave the columns blank if there was no value set for them.

或者,如果您有JavaScript或jQuery将输入添加到表单中,那么您可以给它们一个名称,并在您的php中相应地修改您的查询 - 如果没有为它们设置值,则将列留空。