Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Box Form values retain values after POSTing?

Status
Not open for further replies.

Tarianna

Programmer
Sep 4, 2010
16
US
Hello all,

I hope I'm posting in the right forum. o_O

Data is going from cgi to form...given a value for a drop down box choice, and given also that the drop down box values are dynamically generated. How can you preserve the value of a drop down box choice a user has chosen after it's been error-checked?

Hrmm. I know the value....I can pass the value to the form. I don't know how to tell the form to stop doing 'selected' choice on the drop down list.

Original code:

Active/Discontinued: <select name="activeType">
<option value = "All">All</option>
<option value = "1" selected>Active</option>
<option value = "2">Discontinued</option>
</select>

So, in the code above, Active is chosen by default on the HTML and the script understands that 1 is the internal value for it. The "selected" part of this segment is enforced everytime it is reloaded.

So as a solution I thought I'd define a TMPL_VAR variable, set that as my 'selected' like so:

Active/Discontinued: <select name="activeType">
<option value = "<TMPL_VAR NAME="activeType">"><TMPL_VAR NAME="activeType" selected></option>
<option value = "1" >Active</option>
<option value = "2">Discontinued</option>
<option value = "All">All</option>
</select>

I'd initially set the TMPL_VAR at initial start then set it again as I recall it and set it to the appropriate value. I'm pretty sure I can get this to work but it seems awfully awkward somehow.

Yeah, lol I'm thinking and typing the solutions that are coming to mind....I dont' know if this will work but if anyone knows a more eloquent solution, pls, I'm quite interested.

(FYI: sys admin will NOT allow javascript to run on this machine AND will NOT allow me to use the validate.pm for perl.) :(
 
You should check out the perl CGI module, turns this kind of stuff into one liners.

The other way is to loop through and on every line of the option group check and see if it was passed as the option and if it was then print selected

Code:
for (1..5) {
 print qq~<option value = "$_" ~, $_ eq $input_value ? 'selected' : '', qq~>$_</option>~;
}

or

for (1..5) {
 print "<option value = \"$_\" ";
 if ($_ eq $input_value) {
  print 'selected';
 }
 print ">$_</option>";
}

Not tested at all


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Hrmm..I like that solution...inserting the selected word when the list is being generated. That's actually what I want.

BUT..I've already put in the code to have the top option always selected and inserted the user's choice there (where it starts at the defaulted value for that type of choice.) bahaha.

I'll be sure to remember this the next time a drop down list is needed. :)

Thank you for your reply!
 
Hello Chris,

After reading your post, y'know that's what I do (yeah caught me at a lie *gasp* It was crazy yesterday..sorry.) What I do is I pass parms to the display cgi, it queries, it displays the results.

Right now, if the user wants to sort on another column, it requeries and sorts it by the user chosen column. I read on perldocs that sorting should be left to the SQL engine so I'm opting for that. (Correct me if I'm misguided pls.)

I've abandoned the idea of passing the ref_array of ref_hash between each cgi based on that and this post.

Now, I need to insert some sort of pagination where I will display the xth record to the yth column of each set of results.

I thought that by looking at what page I'm on, I could automatically bring up that value. (i.e. if I"m on the 2nd page bring up the 26th to the 50th row assuming 25 results per page, etc..) and so..I'm faced again with..how do I access say a range of records for a array_ref of hash_refs where array_ref is defined exactly like I described earlier.

I'm thinking something like: *blank stare*

The deferencing part of the hash/array refs is intimidating me to unconsciousness. :( I've read countless books and websites and I think I'm going to have to continue my reading trek.

Any nudge in the right direction is appreciated.
-t

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top