开发者

What is the best way to handle an "other" option in a select box?

开发者 https://www.devze.com 2023-02-19 02:50 出处:网络
I\'m needing to implement a select box that has an \"other\" option. If this is selected the user should be able to type in a different option into a Text Field. My concern is for how to implement the

I'm needing to implement a select box that has an "other" option. If this is selected the user should be able to type in a different option into a Text Field. My concern is for how to implement the save into the database. I'm looking for unique and 开发者_StackOverflowefficient ways to handle this implementation.

I believe the last time I implemented this i had two columns in my table, one for the select box and one for the text field input.

Can anyone think of a better way to do this?


I would have a reference table that populates your select box with fields: id, item, primary. Where 'primary' is a bool indicating if it should be included in your drop down list or not.

Then when users input Other items, the items will be added to your reference table with 'primary' field being false.

The nice thing about this implementation is that it's conceptually clean - there is only one field that stores your item values, and if many of your users are adding a specific field for Other, you can just just flip the 'primary' bool to true and it will appear in your main drop down list.


Assuming you have some kind of a meta data table that drives your select box options, you can add logic that inserts the new "other" option into that table and then reference your newly added item's primary key in your main table that stores the form entered data.

If you don't want to muddy up the meta data table with every "other" submission, then I believe your approach is good - just a separate nullable column that stores "other" value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号