FAQs

How can I disable options in a select drop-down?

The request was for a form for a college site where a course had several options each of which had a limited number of places. Once all the places were taken for one option that should still show in the dropdown but be disabled.
Here's the code used for this form - it can easily be adapted to other similar applications with both select drop-downs and checkbox or radio button groups.
The select drop down is built in a Custom Element element as the Select Drop-down element has no way to set a single option as disabled.

<?php
// get existing sign-ups
$db = \JFactory::getDBO();
$query = "
   SELECT `session`, COUNT(*)  AS count
    FROM `#__chronoforms_data_courses`
    GROUP BY `session` ;
";
$db->setQuery($query);
$session = $db->loadObjectList('session');

// set number of available places
$places = 15;
// set the available options
$options = array(
  1 => 'Session 1 | 1 Oct to 29 Oct | 9.00-11.00',
  2 => 'Session 2 | 1 Oct to 29 Oct | 11.00-13.00',
  3 => 'Session 3 | 3 Oct to 5 Nov | 9.00-11.00',  
  4 => 'Session 4 | 3 Oct to 5 Nov | 11.00-13.00',
  5 => 'Session 5 | 3 Oct to 31 Oct | 9.00-11.00',
  6 => 'Session 6 | 3 Oct to 31 Oct | 11.00-13.00',  
);

// loop through the options and check against the existing inscriptions
$temp = array();
foreach ( $options as $k => $v ) {
  $disabled = '';
  if ( $session [$k]->count >= $places ) {
    $disabled = "disabled='disabled'";
  }
  $temp[] = "<option value='{$k}' $disabled >{$v} Full</option>";
}

// output the HTML for the drop-down
echo "<select name='session' id='session' >
<option value=''>==?==</option>".
implode ("\n", $temp)."
</select>";
?>