Forum Moderators: open

Message Too Old, No Replies

JS Form Suggestions

         

bigbetterirish

9:11 pm on Aug 20, 2007 (gmt 0)

10+ Year Member



I need to make a form that the options change depending on the type of person entering the site.

i.e.

If Consumer is selected, they get the following options
Option 1
Option 2

If Professional is selected, they get the following options
Option 3
Option 4

Let me know if you can help.

Thanks

Fotiman

9:36 pm on Aug 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should not be using JavaScript for this. Instead, you need to look into using a server-side language, like PHP, ASP, JSP, etc. to determine what content to serve up.

heisters

12:15 am on Aug 21, 2007 (gmt 0)

10+ Year Member



Where's the user's type coming from? Once you have that established, say as "userType", do something like this:


var userOptions = {
consumer: [{value: 'option1', label: 'Option 1'},...],
professional: [{value: 'option3', label: 'Option 3'},...]
};

var options = userOptions[userType];

Then you need to create a select element and load the options into it:


var select = document.createElement('select');
options.each(function(o){
var option = document.createElement('option');
option.value = o['value'];
option.textContent = o['label'];
select.appendChild(option);
});
$('container').appendChild(select);

NB. I used the Prototype.js specials 'each()' and '$()' in there, but it's simple to use generic JS functions in their place. Also, it's totally untested but should give you the gist of how to do it.

But really, this probably isn't what you're looking for. You probably want to use a server side language. Or perhaps server-side scripting with a little JS (like Prototype.js' Element.toggle()) to make it spiffy.