CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Select List

 



JFrost
Novice

Jun 21, 2001, 12:45 PM

Post #1 of 8 (1093 views)
Select List Can't Post

This question is a little long, but please bare with me.

Here is my problem.

I have a database that allows users to post and modify their entry. The form is the same for both the post and the modify feature.

I have dropdown menu for some of the fields, e.g.

<select name=food>
<option>Chinese</option>
<option>Mexican</option>
<option>Indian</option>
</option>


When adding a new record there are no problems. When modifying the record I want the value of the field to appear on the dropdown. So I did the following.


Code
<select name=food> 
<option>$db{'food'}</option>
<option>Chinese</option>
<option>Mexican</option>
<option>Indian</option>
</option>

Here is the problem. When the user wants to modify their record, they see the dropdown menu with the first option being the value chosen when adding the record, and the same value again in the dropdown menu, e.g.

<select name=food>
<option>Indian</option>
<option>Chinese</option>
<option>Mexican</option>
<option>Indian</option>
</option>

How can I have it that it should find the value from the dropdown automatically and select it, rather then displaying it twice.

p.s. I told you it was a long question.

thanks you for your help.



mhx
Enthusiast / Moderator

Jun 21, 2001, 1:04 PM

Post #2 of 8 (1091 views)
Re: Select List [In reply to] Can't Post

Hi!

I hope I have a short answer to your long question! If I got you right, your only problem is that you want to be able to set the active item of the selection list. That's quite simple:

Code
<select name=food> 
<option>Chinese
<option>Mexican
<option selected>Indian
</select>

(BTW: There's no </option>, and I think you meant </select> to end your list.)
This way, you can make Indian your selected / active (whatever you like to call it) item. I hope this solves your problem. Smile

-- Marcus



monocle
User

Jun 21, 2001, 1:41 PM

Post #3 of 8 (1087 views)
Re: Select List [In reply to] Can't Post

FYI: Actually, there is an </option> but it is... uh.. optional. however, depending on which level of HTML standards you would like to adhere to, it is NOT optional. For instance, the reletivly new XHTML standard (a sort of XML/HTML hybrid) requires all tags to have the closeing tag with it (except in cases like <img />, <input /> and a few others).

But the "selected" part is right. Wink

monocle



mhx
Enthusiast / Moderator

Jun 21, 2001, 2:08 PM

Post #4 of 8 (1086 views)
Re: Select List [In reply to] Can't Post

Sorry, forgive my ignorance. But I've never seen </option> used anywhere before. I must admit that I'm no HTML-Guru, when I've got to generate HTML from within Perl scripts I usually use the CGI:: primitives. So I don't have to care a lot about whether some tag needs its closing counterpart or not. I've looked up some CGI:: generated code and a (very good) german HTML tutorial and didn't find the closing </option> tag. Now that I've had a closer look at that tutorial I found:

"Ein Abschluß-Tag </option> ist erlaubt, aber nicht zwingend erforderlich."

which is

"A closing </option> tag is permitted, but not mandatory."

in english, so you're absolutely right. (And I'm going to read the whole section next time...)
But anyway, thanks for letting me know that not every option is always an optional option... Smile

-- Marcus



JFrost
Novice

Jun 22, 2001, 6:46 AM

Post #5 of 8 (1076 views)
Re: Select List [In reply to] Can't Post

Thank for the reply.

I think I need to explain myself better.

I have the following code.

<select name=age>
<option value="21">21
<option value="22">22
<option value="23">23
<option value="24">24
<option value="25">25
<option value="26">26
<option value="27">27
<option value="28">28
<option value="29">29
<option value="30">30
<option value="31">31
<option value="32">32
<option value="33">33
<option value="34">34
<option value="35">35
<option value="36">36
<option value="37">37
<option value="38">38
<option value="39">39
<option value="40">40
</select>

The script queries the "age" field from a flat file database. How is it possible to have the value that's in the field selected from the drop down menu. So if the value was "37" the select list would be.

<select name=age>
<option value="21">21
<option value="22">22
<option value="23">23
<option value="24">24
<option value="25">25
<option value="26">26
<option value="27">27
<option value="28">28
<option value="29">29
<option value="30">30
<option value="31">31
<option value="32">32
<option value="33">33
<option value="34">34
<option value="35">35
<option value="36">36
<option value="37" SELECTED>37
<option value="38">38
<option value="39">39
<option value="40">40
</select>

Thanks again.



rGeoffrey
User / Moderator

Jun 22, 2001, 7:54 AM

Post #6 of 8 (1073 views)
Re: Select List [In reply to] Can't Post

Consider this code...


Code
  my %select_text = (1 => ' selected', 0 => ''); 
%selected = map { $_ => 1 } @{$cgi_data{column}};
my $colboxes = join ("\n",
qq ~<select name="column" size="8" multiple>~,
(map { "<option value=\"$_\" $select_text{$selected{$_}}>$_"
} qw (file url referer uagent year
querystring refered parser month
proto port proc searchengine refererdetail browser day
addr login user hostname secdomain lstatus second)),
"</select>\n");

if the array @{$cgi_data{column}} contains qw (url uagent)

then the hash %selected will look like...

%selected = (url => 1, uagent => 1);

So when we build up the string inside the map statement, those two will get the string 'selected' from %select_text, and all the others will get nothing and print just an empty string ''.

Note that when you ask about unselected things like $selected{'day'} perl may complain if you are using -w. Making it -w friendly is left as an exercise for the reader, but the 'exists' function may be involved.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


kencl
User

Jun 27, 2001, 1:05 PM

Post #7 of 8 (1058 views)
Re: Select List [In reply to] Can't Post

Here's the easy way:

Code
<select name=age> 
<option value="21"($db{'age'} == 21)?(" selected"):()>21
<option value="22"($db{'age'} == 22)?(" selected"):()>22
<option value="23"($db{'age'} == 23)?(" selected"):()>23
<option value="24"($db{'age'} == 24)?(" selected"):()>24
<option value="25"($db{'age'} == 25)?(" selected"):()>25
<option value="26"($db{'age'} == 26)?(" selected"):()>26
etc...
</select>

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


Jasmine
Administrator

Jun 27, 2001, 5:37 PM

Post #8 of 8 (1053 views)
Re: Select List [In reply to] Can't Post

Here's an easier one, assuming you're using CGI.pm. And if you're writing CGI stuff, you should be using CGI.pm ;)

Let's assume that you have the list of cuisines in @cuisines. Then, to create the html for the select list with $db{'food'} preselected, just use this:


Code
print scrolling_list( 
-defaults => $db{'food'},
-name => "cuisine",
-values => [ @cuisines ],
-size => 1,
);

Can't get easier than this ;)

And if you wanted the cuisines in alphabetical order, use this -values line instead:


Code
   -values    => [ sort { lc $a cmp lc $b } @cuisines ],

Hope this helps!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives