1

I intend to "bring back" some information about a user in a form, so I have to place that info into the right inputs. Some are in a select structure.

I want to select the right option from that select structure, and for that I do like this:

 <select  class="m-wrap span12" name="gender">
                 {if $obj eq '1'}
                    <option value="">Seleccione</option>
                    <option value="1" selected>Hombre</option>
                    <option value="2">Mujer</option>
                 {elseif $obj eq '2'}
                    <option value="">Seleccione</option>
                    <option value="">Hombre</option>
                    <option value="2" selected>Mujer</option>
                 {else}
                    <option value="0" selected>Seleccione</option>
                    <option value="">Hombre</option>
                    <option value="2">Mujer</option>
                 {/if}
                 </select>

But I want to make it shorter so I don't repeat code lines. I have tried this but with no success:

<select  class="m-wrap span12" name="gender" id="{$obj}">
      <option value="1" id="1">Hombre</option>
      <option value="2" id="2">Mujer</option>
      <option value="0" id="3">Seleccione</option>
</select>

I'm using smarty template and codeigniter.

Thanks in advance, I'm new to programming.

Limon
  • 1,772
  • 7
  • 32
  • 61
  • You can set the selected option by using javascript. Could you elaborate how the use case is - do you want to set the fields after a user has loggend into your site. I am unfamiliar with ` {if $obj eq '1'}` is this a javscript client side template language or some server side bits? – surfmuggle Jan 02 '14 at 21:02
  • @threeFourOneSixOneThree it's smarty and it's server side. – CAPS LOCK Jan 02 '14 at 21:04
  • @threeFourOneSixOneThree what i'm doing is to bring back data that is correct after validation of a form. Data that is correct should appear in the form, and data that is wrong should not appear. So the user has to fill in only the firlds that are wrong – Limon Jan 02 '14 at 21:09

4 Answers4

1
<select class="m-wrap span12" name="gender">
    <option value="1" id="1" <?=($obj == 1) ? 'selected="selected"' : '';?>>Hombre</option>
    <option value="2" id="2" <?=($obj == 2) ? 'selected="selected"' : '';?>>Mujer</option>
    <option value="0" id="3" <?=($obj == 3) ? 'selected="selected"' : '';?>>Seleccione</option>
</select>
Michael D.
  • 264
  • 2
  • 5
1

In this case I'm using just one line of code with the help of form_helper

<div id='myform'>
{form_open('controller/method')}
{form_dropdown('gender',$genders,set_value('gender',$obj),'class = "m-wrap span8 chosen" data-with-diselect="1"')}
{form_close()}
</div>

Where

  1. $genders is list of genders assigned to smarty variable like

    $genders = array('0' => 'Seleccione', '1' => 'Hombre', '2' => 'Mujer')

  2. 'gender' is name attribute value of select tag

  3. set_value() function sets the option selected where option value is equal to $obj

  4. and other class like attributes of select tag

    Note

form_helper must be loaded before display of a template

merdan
  • 1,229
  • 1
  • 11
  • 26
  • So you say I could use this line in my controller? In that case I will have my dropdown menus built from the controller, what about my other input options that are not a dropdown menu? I will just have messy code from the same view in different places... – Limon Feb 05 '14 at 14:38
  • 1
    No no use it in template not in controller! You can use helper function inside templates – merdan Feb 06 '14 at 06:13
  • the thing is that I'm using Smarty...I'm not quite sure mixing this would be a good practice, what do you say? – Limon Feb 06 '14 at 15:33
  • 1
    I'm also using smarty with codeigniter within my current project. for me its best solution. Codeigniter itself suggest to use form_helper in form views so why don't use it in smarty template. I'm submitting forms via ajax with csrf protection enabled then form_open helper function automaticaly adds hidden csrf key input tag inside form – merdan Feb 07 '14 at 05:02
  • 1
    One thing more i put collections like _genders_ in config file and retrieve them with helper like _get_genders()_ to be able to reuse them in other forms so if you want to edit collections you dont need to edit every template it appears, just edit config file also config is localizable. Ex _{form_dropdown('category',get_categories(),set_value('category'))}_ – merdan Feb 07 '14 at 05:59
0

You can do this by making the option's hidden HTML value equal to that option's position within the select list, beginning with zero; then storing the server side value; then setting the selectedIndex of the menu equal to the stored value.

<select  class="m-wrap span12" name="gender" id="gender_field">
    <option value="0" selected>Seleccione</option>
    <option value="1">Hombre</option>
    <option value="2">Mujer</option>
</select>

<input type="hidden" id="gender_obj" value="{$obj}" />
<!-- the above line passes the server-side variable into the
  -- HTML for JavaScript to use -->
<script>
(function (){
var gender_field = document.getElementById('gender_field'),
    gender_obj   = document.getElementById('gender_obj'),
    n;

    if (!gender_field || !gender_obj) return;
    n = gender_obj.value - 0;
   /* will be NaN if nothing (or nothing numerical) is there,
    * such as on the first visit */
    gender_field.selectedIndex = gender_field.options[n] ? n : 0;
})();
</script>
Joseph Myers
  • 6,434
  • 27
  • 36
  • Hi, I'm working with mvc, so I don't want to mix what's in the view with php. I'm gonna use javascript too but later on, first I have to do this on the server side in case user doesn't have Javascript active (This is not very often, but the site should work in all environments) – Limon Feb 04 '14 at 16:25
0

I could finally achieve this. Using Smarty the sintax can be:

<select  class="m-wrap span8 chosen" name="gender" data-with-diselect="1" value="">
   <option value=" " {if $okInfo['gender'] eq ' '}selected{/if}>Seleccione</option>
   <option value="1" {if $okInfo['gender'] eq '1'}selected{/if}>Hombre</option>
   <option value="2" {if $okInfo['gender'] eq '2'}selected{/if}>Mujer</option>
</select>

So that way, if the user selects from a dropdown menu a certain option but then doesn't pass form validation, the data that he entered and it's ok should be retreive in the same way. So for a select html tag, this is the way of "bringing back" the data he had entered.

Limon
  • 1,772
  • 7
  • 32
  • 61