We have a form with some optional values, which are contained in a dropdown list. When a new user opens the form, the default value on the dropdowns should be '-select-'. The value that the user picks gets sent to our database. This all works fine right now, except we do not have '-select-' listed as the first option, the default is instead the first option as listed in our database.
I am trying to add '-select-' but I receive a database error when I submit the form with '-select-' still chosen.
<div class="control-group">
<label class="control-label">Pricing Profile: </label>
<div class="controls">
<?php
echo form_dropdown('pricing_profile_id', $pricing_profiles,
$profile->pricing_profile_id,' class="span3" id="pricing_profile_id" ');
?>
</div>
</div>
I tried adding in this text above the "echo form_dropdown" but it does not seem to work:
array_unshift($pricing_profiles, array(0 => "-select-"));
Thanks
( '' => 'Select' )
HTML does not support "null" values in the way that PHP does, because it is a markup language (XML) rather than a programming language such as PHP.
Therefore you cannot give it a NULL value. The closest you can come to it is either by looking for a value of "NULL" in PHP and setting its value to NULL, or simply allow the option's value to be blank, evaluating blank to NULL in PHP.
In your controller where you are populating $pricing_profiles add the select there. For example if you're passing the data to the view in $data it would look like this:
$data['pricing_profiles'] = whatever you are doing to populate this array;
$data['pricing_profiles'][0] = '-select-';
Then in your form validation you do a callback function
$this->form_validation->set_rules('pricing_profile_id','Pricing Profile',callback_checkPricingProfile);
function checkPricingProfile($pricing_profile_id)
{
if($pricing_profile_id==0)
{
return false;
}
}
So I am the CEO of the company and my CTO was out today (it's just the two of us in the company right now, so I try to code when possible), which is why I asked this question. However turns out he came in and we looked at the answers, all very much appreciated! With the way our controller is set up we found a slightly different solution.
In the view file we added:
array_unshift($pricing_profiles, "-select-");
Within the controller we create an array called $companies_profiles_data, it contains several values, one of which is pricing_profile_id. We added a ternary operator to the line where the value is set.
'pricing_profile_id' => (strcmp($this-> input-> post('pricing_profile_id'),'0')==0)
? NULL : $this-> input-> post('pricing_profile_id'),