[Ajax] Drop Down Dependant Fields In A Form

Hello there. I’m new in Yii and AJAX.

I wanted to modify the standard contact form with an drop down list with a stable values list. I wanted to make that depending to the selected value of the drop down list the other fields would show/hide dynamically without need to re-load the form or page.

Legend of shortcuts used in example:

DD - Drop Down list

AF - Avaliable (visible) fields

Example:

[b]

DD: Private person[/b]

AF -> First and last name, e-mail[i]

[/i]

[b]

DD: Company[/b]

AF -> Name, Company, e-mail[i]

[/i]

Hi

Here is your solution Creating dependent dropdown !

Cheers!

thanks, but the case is more complicated and it can’t be solved this way. The other fields in form should be dependant from the value of dropdown and not other way

Take a look at http://www.yiiframework.com/doc/api/1.1/CHtml#ajax-detail

You can create the javascript function that will be called on dropdown list change and replace the form elements with new values.

as I mentioned before, the values of the drop down should have static values and depending from the one with get selected, the proper fields of the form should show or hide

Looks like what we’ve got here is failure to communicate ;)

You can create js function that will change anything in form based on the selected value from dropdown list - is this the required behavior? You can use it to replace the fields, overwrite the fields’ values, hide or show fields etc.

I’m not sure why you are not happy with the answers so far. Please provide us with more details.

I’m appreaciating all your help given, but it’s kinda hard for me to write exactly how it should look… however I will try.

By example:

I have a form with following fields:

name: text

dropdown with values "service" and "product"

(some more feilds go here)

depending from the dropdown selection, I would like that the remaining fields would corresponding to it show or hide (dynamically change with AJAX / JQuery) that the user wouldn’t need to reload or submit each time the pag.

The actual definition of DropDown in CHTML have problems with handling more complex code