Forum Moderators: open

Message Too Old, No Replies

Dropdown menu - trying to remember value after submitting

         

midoriweb

4:37 am on Feb 18, 2007 (gmt 0)

10+ Year Member



Hey everyone,

I have a form in which a user enters their information. One of those files looks like:

<input type="text" size="25" maxlength="45" name="BillingCity" value="<%=Request.Form("BillingCity")%>" style="<%=HighLight_Field_On_Error("BillingCity")%>">

Now... the value of that input type is: <%=Request.Form("BillingCity")%>. This is important because if the user tries to submit the form but there are errors, the page will load again and whatever they entered will be saved and they do not need to re-enter this field again.

But... how can I do this with a drop down menu?

Right now I have:

<SELECT id="BillingState" name="BillingState" style="<%=HighLight_Field_On_Error("BillingState")%>" value="<%=Request.Form("BillingState")%>">
<OPTION value="">Select State</OPTION>
<option value="AA">AA</option>
<option value="AE">AE</option>
</select>

But when I use that a user will have to re-choose the state they entered if they're form submission returns an error.

Is it possible to remember the state with the way I'm doing this? I'm sure it is... I just can't figure it out.

daveVk

7:13 am on Feb 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Guessing a bit, try

<SELECT id="BillingState" name="BillingState" style="<%=HighLight_Field_On_Error("BillingState")%>" >
<OPTION value="">Select State</OPTION>
<option value="AA">AA</option>
<option value="AE" selected>AE</option>
</select>

That is take value attribute off select, and put "selected" on default state

midoriweb

10:18 am on Feb 19, 2007 (gmt 0)

10+ Year Member



Hi Dave,

Thanks for the help. I ended up figuring it out from searching some other forums for similar questions. I had to use:

<option value="AA"<% If Request.Form("BillingState") = "AA" Then %> selected="selected"<% End If %>>AA</option>

:)

rocknbil

9:53 pm on Feb 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah so now you have 50 If/then's in your state list!

My ASP/VBscript is a little rusty, so bear with me. What you want to do here is build that list dynamically, that is, make an array of the states and cycle through them to print, THEN if the one in Request.Form matches, add selected. Perl-y example, where %qs equates to variables in Request.form:

@states = ('AL','AK','AZ','AR','CA');


$select = qq¦<select name="state" id="state">
<option value="">Select</option>
¦;
foreach $st (@list) {
$select .= qq¦\t<option value="$st"¦;
if ($qs{'state'} eq $st) { $list .= ' selected'; }
$list .= qq¦>$st</option>\n¦;
}
$list .= "\t<\/select>\n\n";

midoriweb

10:48 pm on Feb 19, 2007 (gmt 0)

10+ Year Member



Hi Rockinbil,

Yes... you're right. We actually have 54 IF statements for the state fields due to military states (AE, AP...) and so on.

I don't think all of our if statements will cause any sort of problems though. This script is only called on a select few pages on our website and I doubt this would ever cause a noticeable performance hit.

But... you're way would probably be the optimal way of doing things. However, I'm not a programmer, so changing your code would be pretty tough for me. I understand the logic behind what you wrote... but there's other changes I would need to make to make it work correctly. For example... we'd want our state of CA to be listed to the end user as California. Your script will currently only display CA to the user. I wouldn't know where to begin to tie the full state names into the abbreviated state names.

Question though... how would your script be better then a bunch of if statements? I understand creating an array like you did creates less code on our page... but the array still does if statements just like my code does right? So there's no performance increase... just the code cleaner then what I'm doing right?

rocknbil

12:09 am on Feb 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are correct. If it works . . . it works. So all the follwing is for discussion only.

The advantage is it make for a smaller file size and most importantly it's easier to maintain.

Secondly if this is a function that can be used a lot from different pages or scripts, you could include the stateList function as required, allowing many pages to use a centralized code.

The way you would do it with different values would be to create TWO arrays:

@states = ('AL','AK','AZ','AR','CA');
@stateNames = ('Alabama','Alaska','Arizona','Arkansas','California');

Thin in your if use the value in @stateNames between <option> and </option>.

Or if it's just for Cali add an if only on that part:


foreach $st (@list) {
$select .= qq¦\t<option value="$st"¦;
if ($qs{'state'} eq $st) { $list .= ' selected'; }
$list .= '>';
if ($st eq 'CA') { $select .= 'Celifornia'; }
else { $select .= $st; }
$st</option>\n¦;
}

rocknbil

9:08 am on Feb 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You got me going. :-) Got out my oil can, tested this out - note the comments in the header. Paste it into aspstates.asp and upload it to a Windows server.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>An ASP State List</title>
<!-- Begin by defining state list in the head of the document.
This allows you to add the statelist code anywhere in
the document. Make sure all of the items in () and in
the doctype declaration are on ONE LINE. There's
probably a way to define items in an array as multi
lines without using indexing (stateList[0]="AL", etc,
which is a lengthy list, increasing maintenance) but
it's not coming to me at the moment . . . -->
<%
Dim stateList
stateList = Array("AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL","IN","IA","KS","KY",
"LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","OH","OK","OR",
"PA","RI","SC","SD","TN","TX","UT","VT","VA","WA","DC","WV","WI","WY")
%>
</head>
<body>
<p>Now, build the list using stateList:</p>
<form method="post" action="aspstates.asp">
<select name="BillingState" id="BillingState">
<option value="">Select</option>
<%
For Each item In stateList
Response.Write("<option value='" &item & "'")
If Request.Form("BillingState") = item Then Response.Write(" selected")
Response.Write(">")
If item = "CA" Then
Response.Write("California")
Else Response.Write(item)
End If
Response.Write("</option>" & VbCrLf)
Next
%>
</select>
<input type="submit" name="submitButton" id="submitButton" value="test selected values">
</form>
<%
' This section is for testing, not needed in practical application
Dim billstate
If Request.Form("BillingState") <> "" Then
billstate = Request.Form("BillingState")
Response.Write(billstate & " was selected")
Else Response.Write("Nothing Selected")
End If
%>
</body>
</html>

midoriweb

8:59 am on Feb 23, 2007 (gmt 0)

10+ Year Member



Hey Bill,

You really did code everything? :)

I see that it works... but the problem is, we would need every state to have the short 2 letter name and full name... so your code of:

If item = "CA" Then
Response.Write("California")
Else Response.Write(item)
End If

Would need to become:

If item = "CA" Then
Response.Write("California")
Else If item = "NY" Then
Response.Write("New York")
Else If item = "FL" Then
Response.Write("Florida")
Else If item = "TX" Then
Response.Write("Texas")
Else Response.Write(item)
End If
.....

And so on. Each state would need an if statment.

I think based on our needs, doing it the way I have it now is the best way and keeps the code the clean. But, if someone only needs the 2 digit states and doesn't need to compare 2 digit to full text states, then your code is the best choice.

Thanks for posting the code though... it's nice to study something new and see how it works :)