in reply to Display (print) selected value

You have to add "selected" to the appropriate option tag. Here is one way to do it:
print " <form action=test.cgi method=post>\n"; print " <center><select name='selected' size=1>\n"; print " <option value='$item1'".($input{selected} eq $item1 ? " select +ed" : "")."> $item1</option>\n"; print " <option value='$item2'".($input{selected} eq $item2 ? " select +ed" : "")."> $item2</option>\n"; print " <option value='$item3'".($input{selected} eq $item3 ? " select +ed" : "")."> $item3</option>\n"; print " <option value='$item4'".($input{selected} eq $item4 ? " select +ed" : "")."> $item4</option>\n"; print " <option value='$item5'".($input{selected} eq $item5 ? " select +ed" : "")."> $item5</option>\n"; print " <INPUT type=submit name=submit value=Select\n"; print " </form><p><br>\n"; print " </select>\n";

Replies are listed 'Best First'.
Re: Re: Display (print) selected value
by bigj (Monk) on Oct 21, 2002 at 09:04 UTC
    Allthough I would suggest to remove some redundancy in the code to make it a bit more readable:
    print qq|<form action="test.cgi" method="post">\n|, qq|<center><select name="selected" size="1">\n|; foreach ($item1, $item2, $item3, $item4, $item5) { my $selected_flag = $input{selected} eq $_ ? "selected" : ""; print qq|<option value="$_" $selected_flag> $_ </option>\n|; } print qq|<INPUT type="submit" name="submit" value="Select"\n|, qq|</form><p><br>\n|, qq|</select>\n|;
    Of course, I'd like also recommend to use the CGI module.

    Greetings,
    Janek

Re: Re: Display (print) selected value
by Donnie (Acolyte) on Oct 17, 2002 at 22:59 UTC
    This is looking good! However, I am still experiencing the same problem - or maybe I just don't know how to write the "print" statement so that the selected value displays onscreen when the "submit" button is pressed. How would you display the output when running this code - that is - showing what was selected by the user?

    Thanks again