Page 1 of 1

Dynamic HTML-Form generation

Posted: Fri Jun 27, 2008 12:17 pm
by kingen
Good evening everyone!

I'm not sure if I should post this thread in this or the Application design forum, hope i'll get it right.

At the moment i am building an application, where users should be able to generate their own custom HTML-Forms. I'm not sure how i should implement this. The first i think of is a database structure like this:

Code: Select all

 
 
USER ----> FORM ----> FORM_HAS_ELEMENTS <--- ELEMENTS <--- ELEMENT_VALUE
                                                                                     ^
                                                                               ELEMENT_ORDER
 
FORM_ANSWERS
 
Each user can have many form, each form can have many elements(like textarea, checkboxes and so on), the element_order table stores the order in which the elements will appear at the final page. The element_value table holds every value for each element. The form_answers table should contain all the answers to the form, and has a reference to elements to link this.

Well, as you see i'm totally lost on this.

Hope you'll get the idea of what i want to establish.

Thanks for any replies!

Re: Dynamic HTML-Form generation

Posted: Fri Jun 27, 2008 12:52 pm
by tecktalkcm0391
Use a multi-dimentional array for the fields and just have it print out the the form in a foreach loop

Code: Select all

 
$fields = array(0 => array('type' => 'textbox', 'name' => 'username', 'length' => '50'), 1 => array(...));
foreach($fields as $field){
    if($field['type'] == 'textbox'){
       $field = '<input type="textbox ';
   } elesif(...){
 
   } 
   ...
}
 

Re: Dynamic HTML-Form generation

Posted: Sun Jun 29, 2008 4:50 am
by kingen
Yeah, writing them out isn't a problem. The problem comes to when i should save it to the database. I just can't find a good and scalable way to do that.

Re: Dynamic HTML-Form generation

Posted: Sun Jun 29, 2008 4:55 am
by s.dot
I think you should have two tables linked by a primary key of the form created.

A table for created forms
A table for elements of that form (their type and perhaps attributes and default values)

Re: Dynamic HTML-Form generation

Posted: Sun Jun 29, 2008 10:46 pm
by tecktalkcm0391
If you are just saving the user's form, and do not need to access anything specific from it, then just use serialize and unserialize... it will make it so you can insert it to the database, then make it back into array when you retrieve it..

Re: Dynamic HTML-Form generation

Posted: Mon Aug 25, 2008 8:42 am
by marcth
I agree with tecktalkcm0391, serializing and un-serializing the HTML form is the easiest way. You could also have a USER_FORM table and a USER_FORM_FIELD table; the USER_FORM table describes the <form> attributes and links it to a particular user. the USER_FORM_FIELD table describes each form element/attributes and is link to the USER_FORM table.

Thanks,
Marc