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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show User State in DropDown List.......

Status
Not open for further replies.

FRANDAZZO

Programmer
Nov 29, 2000
60
US
Hello,

I have a dropdown list(combo box) that has all the US states listed in it. The value that gets sent to the Database would be "NY" if the user selected "New York" from the list.

Here is what my dropdown list is for example:
<select name=&quot;States&quot;>
<option value=&quot;FL&quot;>Florida
<option value=&quot;NY&quot;>New York
</select>

Now we have an edit screen to edit a users info. This is the question--- How do I get the dropdown list to show the correct state the user lives in if I am only storing the value &quot;NY&quot; in the database.


I know of one way:
<option value = &quot;NY&quot; <% if myState=&quot;NY&quot; then response.write &quot;Selected&quot;>New York

but I would have to do that for each option tag. And the client does not want to store the option values in a database so I can build the dropdown list by doing a loop.

How am I going to do this.....

Thanks....
 
Hi FRANDAZZO,

you can use Javascript to set the default select object.
here the example,

<html>
<head>
<script>
<% mystate = &quot;NY&quot; %>
function setDefault()
{
for (i=0; i<document.formname.States.length; i++){
if (document.formname.States.options.value == '<%=mystate%>')
document.formname.States.options.selected = true;
}
}
</script>
</head>
<body onload=&quot;setDefault()&quot;>
<form method=&quot;POST&quot; name=&quot;formname&quot;>
<p><select name=&quot;States&quot;>
<option value=&quot;CT&quot;>Connecticut</option>
<option value=&quot;FL&quot;>Florida></option>
<option value=&quot;NY&quot;>New York</option>
<option value=&quot;NJ&quot;>New Jersey</option>
</select>
</form>
</body>
</html>

The Javascript function SetDefault() will get call when the page is load, first it will search the value of all the option tags and find the one match (mystate). If match, javascript will assign that option to be selected.

hope this helps, Chiu Chan
cchan@gefmus.com
 
Hi FRANDAZZO,

here the function setDefalut() again

function setDefault()
{
for (i=0; i<document.formname.States.length; i++){
if (document.formname.States.options[i].value == '<%=mystate%>')
document.formname.States.options[i].selected = true;
}
}

hope this helps
Chiu Chan
cchan@gefmus.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top