View Full Version : Creating Dynamic Forms

08-18-2004, 04:25 PM
I want to a create a form that generates the next question based on what the answer to the previous questions was. The next question should not be visable until the previous question has been filled in, but some questions will have boxes which the viewer must fill, and others will be drop down boxes.

I'm guessing it will be an adaption of something like this?

I would prefer to write this in JavaScript as I've done some JavaScript before but I'm not too sure on where to start with something like this? Has anyone got an example of something like this? I can't seem to find one anywhere:(

08-18-2004, 09:50 PM
The difficulty of this will depend on the complexity of your 'decision tree'. That's probably the best place to start is by creating your decision tree by hand, and the determining how you want to represent it in your Javascript. Depending on the complexity, you may want to take an Object Oriented approach to this.

You may even feel that a database would be helpful in extending and maintaining this. In this case you'd want to mix in a little bit of PHP with MySQL as the database.

Your solution could be really simple (where everything is hardcoded) or somewhat complex (where everything is variable with pointers). The part that will be difficult is if you're using text boxes as answers to determine the next question. How do you know when the user is finished typing? Perhaps you could convert textboxes to radio/checkbox buttons? Or ensure that the user clicks/tabs out of the text box when they have completed typing (then you can use the onChange event handler).

The link you have posted to the DynamicDrive script would be a good start. Try and understand the concept behind that code and that might give you some good direction. I don't think there's going to be much in the 'plug-n-play' department for what you want exactly, so you're going to have to write this custom script yourself.

As far as the implementation, you may be able to encapsulate each question as a DIV (assign an ID to each DIV to take advantage of the DOM) and show/hide them using the style properties: visibility(visible/hidden) and display(none/inline/block).

Hopefully I've given you some key words in this post for you to do some research on using Google and that DynamicDrive script. Perhaps someone else can provide you with some more information.

If you have any other questions, feel free to post them here.

Good luck,

08-19-2004, 05:20 PM
sad69: Thanks for the advice, after reading your response I did a bit more searching and found this script http://www.yxscripts.com/cs/chainedselects.html which I've decided to use instead which does almost everything I need. Now I've had this working fine, then changed a few things and it stopped working, and for the life of me I can't figure out what I've changed. I'm getting the following error message now.

Error: 'document.forms.0.field1' is null or not an object

What does that error message mean?

I can post the whole code if anyone wants to help me but the rules state that sort of question isn't allowed.