Options menu

Discussion in 'HTML' started by Fernie, Dec 15, 2004.

  1. Fernie

    Fernie Guest

    Today, I began experimenting with an 'options menu':

    <SELECT name="CHOICES">
    <OPTION>YES</OPTION>
    <OPTION>NO</OPTION>
    <OPTION selected>MAYBE</OPTION>

    It took me a while to figure out how to set and read values. I've got it
    working but I am surprised an how cumbersome my solution is and I'd like to
    know if I took the wrong approach.

    TO PROGRAMATICALLY READ A USER SELECTED ITEM:

    My cgi reads the selected value of the 'CHOICES' control. This is easy and
    makes sense.

    TO PROGRAMATICALLY SELECT AN ITEM TO DISPLAY:

    Now for the cumbersome part. My cgi must insert the text 'selected' next to
    the option that I wish to select when displayed. To me, it makes more sense
    to assign a value to CHOICES instead. Am I missing something obvious here?

    I am guessing that web development environments such as PHP, Perl, and
    others have built in routines to make the handling of 'option menus' simpler
    than having to modify the html yourself. Is this correct?

    Thank you,

    Fernie



    --

    ELKNews FREE Edition - Empower your News Reader! http://www.atozedsoftware.com
     
    Fernie, Dec 15, 2004
    #1
    1. Advertising

  2. Fernie wrote:

    > It took me a while to figure out how to set and read values. 


    Then it might be worth investing some time in the HTML specification.

    <http://w3.org/TR/html4/>

    Its pretty readable.

    > I am guessing that web development environments such as PHP, Perl,
    > and others have built in routines to make the handling of 'option menus'
    > simpler than having to modify the html yourself. Is this correct?


    There are various prewritten modules for generating such things, but its
    reasonably trivial to do yourself.

    This Perl code for example (although you would usually generated it from a
    database or similar rather then hard coding it):

    $template_data->{options} = ['one', 'two', 'three'];
    $template_data->{selected} = 'one';

    Although with this Template::Toolkit template:

    <select name="myOptions">
    [% for opt = options %]
    <option [% if opt == selected %]selected[% END %]>
    [% opt %]
    </option>
    [% END %]
    </select>

    (That's written from memory so it might not be entirely correct).

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
     
    David Dorward, Dec 15, 2004
    #2
    1. Advertising

  3. Fernie

    Fernie Guest

    "David Dorward" <> wrote in message
    news:cpq67k$mlc$1$...
    > <select name="myOptions">
    > [% for opt = options %]
    > <option [% if opt == selected %]selected[% END %]>
    > [% opt %]
    > </option>
    > [% END %]
    > </select>
    >
    > (That's written from memory so it might not be entirely correct).


    Thanks very much for providing me with the template sample that sets a
    value. It gave me some ideas. I'm curious, is the above a snippet of a
    file that gets read into memory, modified and then returned to the user?

    Although my original attempt was awkward and in need of streamlining, I see
    that I was editing the html source correctly.

    I basically load an html file that I use as a template into into memory and
    then the sections between %% with either a blank or the text 'selected'.
    Then I return the modified memory resident data:

    <SELECT name="CHOICES">
    <OPTION%choices_yes%>YES</OPTION>
    <OPTION%choices_no%>NO</OPTION>
    <OPTION%choices_maybe%>MAYBE</OPTION>

    I'll streamline my method above by writing a function that takes a set of
    parameters and returns the generated html indicating the selected item.
    I'll loop through each item like your template snippet does:

    String GetOptionMenu(String sOptions, String sSelectedItem)

    Best regards,

    Fernie



    --

    ELKNews FREE Edition - Empower your News Reader! http://www.atozedsoftware.com
     
    Fernie, Dec 16, 2004
    #3
  4. Fernie wrote:

    > Thanks very much for providing me with the template sample that sets a
    > value. It gave me some ideas. I'm curious, is the above a snippet of a
    > file that gets read into memory, modified and then returned to the user?


    Yes, see Template::Toolkit <http://search.cpan.org/>

    --
    David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
    Home is where the ~/.bashrc is
     
    David Dorward, Dec 17, 2004
    #4
  5. Fernie

    Guest

    Fernie <Fernie@not_thisdocsignings.net> wrote:
    > Today, I began experimenting with an 'options menu':
    >
    > <SELECT name="CHOICES">
    > <OPTION>YES</OPTION>
    > <OPTION>NO</OPTION>
    > <OPTION selected>MAYBE</OPTION>
    >
    > It took me a while to figure out how to set and read values. I've got it
    > working but I am surprised an how cumbersome my solution is and I'd like to
    > know if I took the wrong approach.


    You'll probably find it much easier to add a "value=" attribute:

    <OPTION VALUE="Y">Yes</OPTION>

    Then, if order isn't important, and you're working with perl:

    %options = ( 'Y' => 'Yes',
    'N' => 'No');

    PHP is: $options = array( 'Y' => 'Yes') but the same principle.

    Once it's in an associative array, you can simply do something like this:

    $selected = array('Y' => 'selected="selected"');
    foreach($options as $value => $label){
    echo sprintf("<option value="%s" %s>%s</option>",
    $value,$selected[$value],$label);
    }


    It gets a little tricky if the order is important, but it's the same basic
    idea.

    Yea, it is a little cumbersome IMO, would be nice if you could do this in HTML:

    <select name="FOO" value="BAR"><option ... >...<option value="BAR">...</option

    BUT that wouldn't work so good for multiple values.
    > TO PROGRAMATICALLY SELECT AN ITEM TO DISPLAY:
    >
    > Now for the cumbersome part. My cgi must insert the text 'selected' next to
    > the option that I wish to select when displayed. To me, it makes more sense
    > to assign a value to CHOICES instead. Am I missing something obvious here?


    How would that work for <select multiple="multiple"> ... ?

    (Would be nice though :) )

    Jamie
    --
    http://www.geniegate.com Custom web programming
    (rot13) User Management Solutions
     
    , Dec 25, 2004
    #5
  6. Fernie

    Fernie Guest

    Jamie,

    Thank you for the suggestions.

    Best regards,

    Fernie



    --

    ELKNews FREE Edition - Empower your News Reader! http://www.atozedsoftware.com
     
    Fernie, Dec 28, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cliff  Martin
    Replies:
    1
    Views:
    3,042
    Larry Smith
    Jan 31, 2007
  2. jacksu
    Replies:
    0
    Views:
    512
    jacksu
    Oct 9, 2007
  3. Utkado
    Replies:
    2
    Views:
    2,118
  4. Bryan Richardson
    Replies:
    6
    Views:
    289
    Bryan Richardson
    Feb 25, 2008
  5. Sijo Kg
    Replies:
    1
    Views:
    102
    Justin Collins
    Jan 7, 2009
Loading...

Share This Page