0

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!

John Ayers
  • 509
  • 1
  • 11
  • 33

2 Answers2

1

If CodeIgniter uses Bootstrap, as it appears it does, then off the top of my head:

<?php
$countries = array(
  'CHOOSE' => 'Choose a country'
  'CA' => 'Canada',
  'MX' => 'Mexico',
  'US' => 'United States'
);
<?php echo form_dropdown('country',$countries,'CHOOSE','class="form-control"','$("[label=CHOOSE]").attr("disabled", "disabled")'; ?>

as per the instructions in this answer: How do I make a placeholder for a 'select' box?

Community
  • 1
  • 1
justathoughtor2
  • 141
  • 1
  • 5
0

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.

Daniel Waghorn
  • 2,997
  • 2
  • 20
  • 33
  • That does work, so thank you, but not exactly as id hope. Canada still shows up as the default before you have selected anything. So if i save my user page without change it, Canada is the country. – John Ayers Jul 07 '15 at 20:50
  • What is `$user->CountryCode` set to as default? You may have to write some logic so that if it is not set by the user to pass it to the form helper as `'--" disabled="disabled'` then it should select the 'Please select a country' option. – Daniel Waghorn Jul 07 '15 at 20:54