Allowing the creation of custom forms and storing the data - php

I'm building a site and need to allow users to be able to create custom forms. The part I'm having trouble with is how to store the data from these forms. Since they are user created fields, there will be no corresponding field in the database. What would be the best way of going about this? I assume modifying the table is the incorrect way of doing so. Would putting all the data into an array, serializing it, and then storing it in a field be a good way?

Would putting all the data into an array, serializing it, and then storing it in a field be a good way?
While that would store the data, it would not make the individual elements queryable. It's an astoundingly bad long-term idea.
Take a look at the entity-attribute-value model. You should be able to represent the fields in a form and even the data filled in to the form using EAV backed up by some rigid foreign key checks.
Now, EAV isn't perfect. It can be tough to create certain queries against it because of MySQL's limitations, but that's nothing a bit of external code can't fix. It'll still be ten times better than serialized data.

maybe you can serialize the values of the complete form and storing it in a blob to the database.

Maybe you can provide a solid Form builder framework (or modify an existing solution) where every user created field controlled by you like this:
//input elements named by a custom namespace:
`<input type="text" name="mycustomelement[user_defined_name]" />`
and there you go: when user submits the form, you can catch all 'mycustomelement' element then you can serialize it to store in a db.

I would store a table userforms {id, user, index, fieldname, fieldtype} and then userformresponses {id, userformid, fieldname, response}

Related

In what format should I store data of a large php form with few dynamic fields

I have a large php form with dynamic fields and there can be additional fields added after releasing into production. How do I store this large data into mysql database using php so that it can handle dynamic fields and additional fields. Example of a form can be seen in the link below.
http://form.jotform.me/form/50655815937465
The above link doesn't contain dynamic fields for now but it may contain later.
The main form is huge so we divided into different sections. Need to handle this as well. Also need a save draft concept where the user can save the data and load it later when he gets a chance to complete it.
There's a lot of different ways to handle this, but I'll make a simple recommendation
For the static fields
Create a column in your table for each static field
Save each static field submission in the corresponding column
For dynamic fields
Create an array of dynamic values and call serialize on it
Store the serialized value in a single long text column in the database
To read the values, you will have to call unserialize on it
Pros
Easy to implement
Ability to filter data based on static inputs
Only uses one extra column for dynamic inputs
Supports a large amount of dynamic form inputs
Cons
Difficult to filter data based on dynamic inputs
Alternative solutions
These solutions are significantly more robust but require a much deeper understanding. I'll leave these here for you to research on your own.
Entity Attribute Value (EAV) model
Document-oriented database

EAV in my cakephp

I am working on a new application and I have a need to use an EAV
table style for a few different tables. Primarily the one I am posting
about is my users table. I need to use EAV so that custom user defined
fields can be added/displayed easily.
So far I have a simple table structure, I have a made an ERD for easy viewing.
The approach I am thinking about using is similar to this post on key value pairs.
A simple beforeSave() that matches up fields to the form name.
There is only one issue with this that I am seeing, since I am not storing
the field name(attribute_code from eav_attributes) with the value, how
would I go about matching up the form field with the attribute_code
and ultimately with the value the form field passed in?
I think the approach in the blog is fine. Except instead of looping through the data array directly, you'd have to generate a list of the applicable EAV keys first since you're storing the keys separately. You just need to check that it exists in the data array before setting it so you don't set empty keys.
Basically you'd use three models rather than two, since you need to take in to account the model managing the attribute labels.

Solution for creating dynamic forms

I'm having some trouble deciding which approach I should take. I want to allow users to create their own html forms by choosing different form elements (textfields, textareas, lists, ratios, etc) I guess something similar to http://wufoo.com/ but much more basic. Should I use database tables or create files?
For tables I was thinking to create a table for each form element, e.g
TEXTFIELD TABLE
ID
TEXTFIELD_NAME
USER
...
TEXTAREA TABLE
ID
TEXTAREA_NAME
USER
etc for all form elements
and then just query all of them with..
WHERE user=$user
Or should I generate php files on the server for each form created?
One approach I used for custom forms on a mobile device was to define a table for field types (eg: text, date, money, custom), a table for form templates (with owner, version, and XML for the template data), and a table for form values (as XML) with a foreign key to the template.
Someone may create a form template with the available field types, and the collected data may be stored for that template, and optionally by template version. If the database supports it, you could query into the XML data to aggregate fields across forms.
For presentation, you could use a fixed style, or transform the template or collected fields however desired.
Should I use database tables or create files?
Storing this stuff in a database has the big advantage that you can easily edit them programmatically, and/or populate the user interface used to edit them.
A generated HTML structure you would have to parse first.
So definitely a database.

how to tackle a custom forms database

I'm currently researching a project for the place that I work in. We are trying to create a system that will allow forms to be set up dynamically from a database.
My question is what database structure would best suit something like this?
I currently have a structure of:
forms_form
forms_formfields
forms_formdata
I don't think this is the most appropriate layout for this. Basically to make is make sense I need to be able to make a form within the database that can have infinite fields all customized and have the data when submitted stored in the database.
The proposed structure looks ok to me. form -> field is clearly 1:M relation, so you'll need forms(id,...) and fields(id, form_id, type, ...). field->data is kind of 1:1, so theoretically a separate table is not needed, unless you allow one form to be used with different sets of data. If this is the case, i'd suggest datasets(id,...) and data(field_id, dataset_id, value).

User configurable forms with php and mysql

An online application we are building (php & mysql) requires users to be able to create their own forms for data capture and record this data in a database, respecting the existing ORM's.
If the forms where "hard coded" then we would simply set the db tables up to store the normalised data ourselves however as our users define the form fields contained in the forms, we're not sure what is the best way to proceed to implement this functionality.
Do we need to think about some kind of meta data or data abstraction layer for our DB? Google hasn't been too much help as we're unsure about how we need to go about this.
Any pointers in the right direction would be gratefully appreciated!
Many content management systems address this problem in different ways.
For example, in Drupal, users can create their own custom content (with custom forms) through the CCK module. The module defines different types of fields that the user can create, then generates tables with specific data types to store the data.
Some tips:
Define your field types - Think about giving the users a choice of different field types (e.g., select box, string, radio).
Create tables for user defined fields - Each field type will have a specific SQL data type. Define a table using these data types. For example, a select box might be mapped to an enum and a input text element might be mapped to a varchar column.
Add data to the new tables - use the new tables to store the data in a somewhat normalized way.
Obviously there are many different approaches, but these are just a few suggestions.
I think I've found a solution to my problem, so for all those people who come along a similar problem have a look at the following artcles -
http://www.adaniels.nl/articles/an-alternative-way-of-eav-modeling/
http://weblogs.sqlteam.com/davidm/articles/12117.aspx
Hope this helps.

Categories