Massive dynamic form in PHP: The best approach?
This question is likely to be somewhat longer than SO's average questions, so I'll try to keep i开发者_运维知识库t as short as possible:
I'm designing a dynamic form that users need to fill in and submit. This form is massive, with over 500 possible questions. The questions displayed to the user depend on answers given in previous answers (for example; only if the user checks the 'still in school' checkbox, the school-related questions page will show.)
This form has quite a few requirements:
- Multiple pages, which can be 'turned on or off' depending on the answers given.
- When a user goes back and changes a particular answer, filled groups that will be turned off need to have their questions' answers emptied.
- Some questions are pre-filled according to earlier answers given.
- Forms will need to be saved to the database on every new page request, so users can pause and continue filling the form at will.
The best approach to this would seem to be an OO one; define a class that accepts multiple options (title, type of input to be used, possible answers, etc), and builds the HTML accordingly.
The main problem with this approach, however, is that because questions can 'borrow' information from prior ones, it won't suffice to create an instance for just the questions to be displayed on the page. In fact, the only reliable way is to instance every possible question, everytime the user loads a new page.
This approach is likely to kill PHP performance; and to me, it seems like this is not the proper OO solution.
Are there better solutions to this problem?
In essence, I need a fast and lightweight way to create multi-dimensional array of 500 to 1000 records (class instances?), roughly 10 of which will be displayed on a page at any given time.
Preferably, I want to store the form as an XML document, so any approach that would work well in this aspect would be preferred.
I'm going to describe a solution using a database as the storage mechanism, you can use whatever works, but this will be easier to describe.
Start I'd build a table that holds all of the "Pages" of the survey. This would just be used to group questions, and to define any requirements for display
Second build a table to store every question in your form. This table should hold the base information about what the question is, it's type, answers (if predefined), etc. As well as any information specifying where predefined data might come from if it's dynamic.
Third build a table/tables to hold your users data.
Now that you have all of your data taken care of build a single web page that will load all users data for the form. Use the users data + session data to determine what "Page" from your database needs to be displayed. Using that information pull all questions for the "Page" being displayed and build them into an HTML form for display to the user. Your form should submit back to itself, could have multiple buttons to control Previous, Next functionality. If when your page loads it's been submitted you'll need to store any values POSTED before pulling your user data back out.
That is how I'd solve this problem working from scratch. You can break many of these pieces up into various objects to help encapsulate your data and functionality. But your question to me sounds like you were lost as to where to start so hopefully this will help you solidify things.
If you are able to be flexible with a framework you can use then by using something like Zend framework you could simplify the creation of forms and handling of validation.
Zend Forms is an example of some of the features that are built in to the zend form control. It makes it very easy and it's not a bad thing to be getting some experience with an MVC style framework.
If you can't use a framework for whatever reason i would suggest writing a custom class to handle the form persistance so you can pass the data between several "views" or "pages"
.NET is pretty handy for this using its multi view control :)
精彩评论