How do I add dynamic dropdown in Joomla JForm XML file

forms, joomla, xml

Solution

You can use "sql" type for dynamic data-

http://docs.joomla.org/SQL_form_field_type

like below example-

<field 
    name="link" 
    type="sql" 
    default="" 
    class="articleselectbox" 
    label="Select an article"
    query="SELECT 
    concat(#__categories.alias, '/', #__content.id,'-', #__content.alias,'.html') as value,              
    concat(#__categories.alias, '/', #__content.id,'-', #__content.alias,'.html') as title 
    FROM #__content 
    LEFT JOIN #__categories ON #__content.catid=#__categories.id 
    ORDER BY #__content.title" 
    key_field="title" 
    value_field="value" 
/> 

Problem

Fairly new to Joomla development. Put a folder called Forms in model folder to load the necessary JForm data. Everything is working fine but I need to grab data dynamically from the database to populate a drop down box. ``` <field name="category" type="list" label="Item Category" description="Item Category" class="inputbox" > <option value="1"> Data from database</option> <option value="2"> Data from database</option> <option value="3"> Data from database</option> </field> ``` The above is a rough example. I want the values and option names to come from a database. Do I use a JTable or params and if so how? I much appreciate any help. Thanking you all.

Original source