Forums

Auto-populate a Dropdown Form Field

jodybossert 16 Jan, 2010
Is it possible to auto-populate a dropdown form field? I am creating a form to allow people to vote for the various candidates who have been nominated for our business association via a nomination form I made. Now that the nomination period is over, I would like to auto-populate the voting form from the list of nominees that were saved in the database.
nml375 16 Jan, 2010
Hi,
Yes, it is very much possible. There are a few good examples lurking around in the forum, but I'll see if I can't scratch something from the back of my head..

The code below assumes there are two fields in your database (id and name), and it's named jos_nominees. The SQL-query could probably be better tailored to your specific needs with more details, but this should get you started..
<?php
$db =& JFactory::getDBO();
$query = 'SELECT * FROM ' . $db->nameQuote('#__nominees');
$db->setQuery($query);

if ($nominees =& $db->loadObjectList();) {
  echo "<select name="nominee">";
  foreach ($nominees as $nominee) {
    echo "<option value='" . $nominee->id . "'>" . $nominee->name . "</option>";
  }
  echo "</select>";
} else {
  echo "No nominees available";
}
?>


/Fredrik
yaksushi 26 Oct, 2010
using the above example I would like to populate a dropdown box with a users first and last name the name is found in jos_jpo_athletes with f_vname being the first name and f_nname being the last name. First will this code work? Second where do i put it so I can access it from the dropdown in the from?

Thanks for you help!

<?php
$db =& JFactory::getDBO();
$query = 'SELECT * FROM ' . $db->nameQuote('#__jpo_athletes');
$db->setQuery($query);

if ($nominees =& $db->loadObjectList();) {
  echo "<select name="nominee">";
  foreach ($nominees as $nominee) {
    echo "<option value='" . $nominee->f_vname . "'>" . $nominee->f_nname . "</option>";
  }
  echo "</select>";
} else {
  echo "No nominees available";
}
?>


Second where do i put it so I can access it from the dropdown in the from?

Thanks for you help!
GreyHead 26 Oct, 2010
Hi yaksushi,

I think it should work OK, it goes into the Form HTML box.

Please add
if ( !$mainframe->isSite() ) { return; }
after the first <?php tag tp avoid some ChronoForms problems.


Bob
This topic is locked and no more replies can be posted.