Dynamic dropdown - setting the selected option

jamesreed 04 Dec, 2012
Hi there, I find chronoforms amazing and have used it extensively, but am getting stuck on setting the selected value of a dynamic dropdown - is it possible? or do I have to use javascript?

Say I have a dropdown box with Dynamic ticked + a model id + value and text set, with the custom code below before ShowHTML - how would I have the 3rd option "selected" in the dropdown box?
<?php
$db =& JFactory::getDBO();
$sql="
SELECT id,name from #__users
";
$db->setQuery($sql);
$result = $db->loadObjectList();

$i=0;

foreach ( $result as $row ) {

$id=$row->id;
$name=$row->name;

$form->data['my_dropdown'][$i++]=array("value"=>$id,"text"=>$name);

}
?>


thanks for any help,
James
marklandry 04 Dec, 2012
Hi, sorry to jump in @jamesreed,
Was wondering - I'm using something similar to what you've shared above, but can't get it working.
I've got the dynamic dropdown ticked but nothing happens. Is there some other setting I'm missing here?

Thanx for any help you can provide.

Mark
GreyHead 10 Feb, 2013
Hi James,

I think that the answer to this one is a little bit simpler than your code:
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT `id`, `name`
    FROM `#__users`
";
$db->setQuery($query);
// this line sets the data for the drop-sown options
$form->data['my_dropdown_data'] = $db->loadAssocList();
// this line sets the selected value
$form->data['my_dropdown'] = $form->data['my_dropdown_data'][1]['id'];
?>
I only have two users on this test site so the 1 selects the second user. You could elaborate this code to select a specific user ID, or a user name.

Bob

PS In this example I've set the options in the Dynamic Data tab with Value key: id and Text key: name to match the columns in the #__users table.
This topic is locked and no more replies can be posted.