Options menu

F

Fernie

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
 
D

David Dorward

Fernie said:
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).
 
F

Fernie

David Dorward said:
<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
 
D

David Dorward

Fernie said:
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/>
 
G

Guest

Fernie said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top