View Full Version : dynamic values: radio buttons?

04-18-2012, 06:11 PM
Main question:
Can I use radio buttons on a dynamic form and have the names filled in with the ENUM values of a database column?

A group can only be one type of organization and one size within that organization. So instead of making a separate table to bring these things together, I made one column called "Organization Type" and set it to ENUM with 21 possibilities (7 types, 3 sizes each).

I just started learning PHP a few weeks ago, so please forgive me if this is a question with an obviously simple answer.

Sub question:
If I can use radio buttons, where would I indicate the actual value to be put into the database record?

My initial thoughts are:
do an $orgtype = array( 'value 1', 'value 2', 'value 3'); (php)
create a function that sets the orgtype (js)
add "onClick settype()" to the radio button attributes (html)

is that logical? would it work or am I missing something essential?

ETA (trying to work it out while I hope for a response/feedback):

So far:

$orgtype=$_POST['Organization Type'];

//JS sets var based on ENUM values in database
var orgtype=["Large Corporation","Medium Corporation", "Small Corporation"]

I also see that I'd have to use onchange instead of onclick.

Edit again: I found the answer to value question.

04-18-2012, 08:12 PM
There is a way to pull the actual enums available from a field with a query, but offhand I'm not sure what it is. You'll probably have to pull from information schema. I personally use lookup tables instead of enums as I find them much easier to work with when querying and updating (I'd rather insert than alter).
The logic is identical whether you manually specify it, or if you allow it to be queried:
- Get list of available options
- Populate menu of options
- Do some JS stuff if you want from those options
- Submit that option
- Either
-- Check that the option is valid first then insert
-- Insert and check if it failed to insert due to constraint / enum violation

04-18-2012, 08:18 PM
I don't know what a lookup table is. I will go learn that. Thank you for the feedback.

04-18-2012, 09:46 PM
Its just a table that typically has only one column, and it would be the same type and data used within the enum. It's bound on foreign key restraints that require the entries to come from that table, so random data cannot be entered in a related table.