Search code examples
drupaldrupal-6drupal-fapi

Most efficient way to have a 50 state drop down box in Drupal Forms


I know that one way is to have a table in database with all the states and then you would read it in your form. Is there any easier way in your opinion guys ?

I feel bad asking for something like this since it is so elementary however I would suppose something as simple like this would already be implemented in Drupal.


Solution

  • No need to hit the database. Build yourself a function that returns an array of the states.

    $form['state'] = array(
      '#type' => 'select',
      '#options' => mymodule_states_list(),
      '#title' => t('State'),
    );
    
    function mymodule_states_list() {
      return array(
        'AL' => 'Alabama',
        'AK' => 'Alaska',
        ...
        'WY' => 'Wyoming',
      );
    }