How To Dynamically Show/hide Form Fields Based On Drop Down List Selection

I’m relatively new to Yii, but was able to figure out how to display a drop down list form field that grabs its values (different categories) from the database. The form I’m working with is supposed to let the user create an asset, and the different asset categories are accounts, printers, servers, workstations, routers/switches, and storage.

I’m storing all of these assets in one database table, even though one could argue making a separate table for each category is more efficient. In the table, there are some fields that only apply to certain categories (an account doesn’t have an IP address, but a printer doesn’t have username/password). Because of that, I want to show or hide some of the fields on the create form based on which item is chosen from the category drop down.

I tried following the suggestions from this thread, but couldn’t get it to work. It also doesn’t make complete sense to me because the code just echoes a new field to the screen instead of actually hiding/showing a preexisting field.

Hi Android Guy!

Try this thread.

I tried disabling a textfield based on the change event of a selectbox. May be you can check your drop downlist current value and use the visible property of the form elements to be displayed.