Welcome Guest, Not a member yet? Register   Sign In
set_select problem in form helper
#1

[eluser]xerosis[/eluser]
I have a list of countries ordered alphabetically.

Now what I want is default selected to be "United Arab Emirates", but if user has selected something else submitted with a problem, using set_select it autofills.

The second part works well, meaning if user inputs something wrong in the form, the form comes back and the selected country is assigned, but I do not know how to make a default select when set_select does not exist.

Can someone help me here.

Thanks.
#2

[eluser]CroNiX[/eluser]
show your code. There are several form helpers that work with selects, so we'd need to see which you are doing, or if you are manually creating it.

If you use form_dropdown() helper, you can use set_value() which takes 2 parameters. One of them is for repopulating the form if there is a validation error and the other is the default selected value when the page first loads.

It'd be almost identical to the example for form_dropdown(), except you'd use set_value() for the 3rd parameter.

Code:
$options = array(
                  'small'  => 'Small Shirt',
                  'med'    => 'Medium Shirt',
                  'large'   => 'Large Shirt',
                  'xlarge' => 'Extra Large Shirt',
                );

echo form_dropdown('shirts', $options, set_value('shirts', 'large'));

Which would have the "large" option selected upon initial page load, or repopulate it with whatever value was sent for that field if there is a validation error.
#3

[eluser]xerosis[/eluser]
Sure.
The countries read from a database.

Code:
<label>Country of Residence:</label>
<select name="country">
&lt;?php if(isset($countries) && is_array($countries) && count($countries) > 0):?&gt;
&lt;?php foreach($countries as $country):?&gt;
<option &lt;?php echo set_select('country', $country->name, TRUE); ?&gt;>&lt;?php echo $country->name; ?&gt;</option>
&lt;?php endforeach; endif; ?&gt;
</select>

Notice the set_select works fine. But due to the use of set_select automatically the last item on the dropdown which is "Zimbabwe" gets selected. I want "United Arab Emirates" to be the default selection if set_select does not exist.

Thanks.
#4

[eluser]CroNiX[/eluser]
I'd try it the way I showed. You'd just have to get the $options array in the right format coming from your model, using array(key => value, key => value), where the key is what will be sent with the form and the value is what is displayed as the option text. It tremendously simplifies the logic.
#5

[eluser]xerosis[/eluser]
I see what you are doing but I am sure there has to be a solution for the way that I have done it as well.
If no one responds I will go ahead with the way you mentioned.

Thanks a lot.
#6

[eluser]CroNiX[/eluser]
There is, but you'd have to add even more logic to what you have, which is very inefficient compared to the form_dropdown() helper. You'd have to first see if the form was submitted. If not, in your loop, check to see if the current value of the loop is equal to "United Arab Emirates" and if so, set the 3rd parameter of set_select() to TRUE, FALSE otherwise. If it has been submitted, you'd repopulate it with the last entered value. You currently have the 3rd parameter set to TRUE for all of them, which is why the last one shows up selected. You are actually having them ALL show up selected (view the rendered HTML) by setting that to TRUE for all of them in your loop, but browsers will only show the last one unless it is a multiselect since the standards call for only 1 being selected at a time.
#7

[eluser]CroNiX[/eluser]
Code:
&lt;?php
$country_options = array();
foreach($countries as $country)
{
  $country_options[$country->name] = $country->name;
}
?&gt;
<label>Country of Residence:</label>
&lt;?php echo form_dropdown('country', $country_options, set_value('country', 'United Arab Emirates')); ?&gt;

Or, you could just do it the simple way Smile
#8

[eluser]xerosis[/eluser]
Thanks a lot!! I will use this Smile
#9

[eluser]Aken[/eluser]
To answer your original question, the TRUE parameter in set_select() says that that particular option should be selected by default if a chosen POST value doesn't exist. Since you're using TRUE as the default in your loop, you're specifying every single option as the default. Because of your loop, the last item is the one that keeps that setting, since it's overwritten in each loop iteration.

You'd want to use a conditional check to define the TRUE or FALSE default parameter.

Code:
&lt;?php echo set_select('country', $country->name, ($country->name === 'United Arab Emirates')); ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB