Dynamic HTML-Form generation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingen
Forum Newbie
Posts: 3
Joined: Fri Jun 27, 2008 12:11 pm

Dynamic HTML-Form generation

Post 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!
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Dynamic HTML-Form generation

Post 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(...){
 
   } 
   ...
}
 
kingen
Forum Newbie
Posts: 3
Joined: Fri Jun 27, 2008 12:11 pm

Re: Dynamic HTML-Form generation

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Dynamic HTML-Form generation

Post 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)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Dynamic HTML-Form generation

Post 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..
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: Dynamic HTML-Form generation

Post 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
Post Reply