John Ayers John Ayers - 1 month ago 11
PHP Question

How to assign disabled attribute to dropdown menu in codeigniter?

I am using the form helper from codeigniter to create a dropdown menu. I have looked at http://codeigniter.com/user_guide/helpers/form_helper.html but it doesn't specify how to assign attributes to drop-down menu items. I want to the disabled option to the first one.

As it stands now im using:

<?php
$countries = array(
'CA' => 'Canada',
'MX' => 'Mexico',
'US' => 'United States'
);
?>
<?php echo form_dropdown('country',$countries,$user->CountryCode,'class="form-control"'); ?>


How can I add a first option that says choose a country that is disabled so the user cant pick it once they open the dropdown? I know I could just add a blank one to the list but then its a pick-able option.

Thanks for the help!

Answer

CodeIgniter doesn't actually support that via the form helper. Personally I'd just hardcode the form if I needed this kind of thing.

There is however a small hack / bug that exists that you can use to get what you want. Try this for your countries array:

$countries = array(
  '--" disabled="disabled' => '** Please select a country **',
  'CA' => 'Canada',
  'MX' => 'Mexico',
  'US' => 'United States'
);

Then feed the array to the form helper as you have already done and see if that works.

Comments