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!

simple combo prob

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
hi - have a combo that lists birth years that are no younger than 18 and no older than 75.

my problem is getting the combo to stay selected
when someone chooses a year.

i am requesting the value x_yearid,
but dont know how to put 'selected' after the option tag
when a year is chosen.

hope i am being clear - thanks again MG

Code:
<%
x_yearidList = "<select name='x_yearid'><option value=''>Year</OPTION>"
currdate = date()
yearlimit = DateAdd("yyyy", -18, currdate)
yearlimit = (datepart("yyyy",yearlimit))
yearstart = yearlimit - 57

For x_yearid = yearstart to yearlimit
x_yearidList = x_yearidList & "<option value='" & x_yearid & "'"
        x_yearidList = x_yearidList & ">" & x_yearid & "</option>"
    Next

x_yearidList = x_yearidList & "</select>"
Response.Write x_yearidList
%>
 
I don't understand exactly what you are wanting.

I think you are saying that you want a year selected from the dropdown, submit a form, the year that was selected becomes the default selected on the refresh of the page?




[monkey][snake] <.
 
well - if i request x_yearid and then response.write x_yearid it is blank

any ideas why
 
Two things come to mind. One, your <select> tag isn't within the <form> tags, OR, you are not submitting the form before trying to request information from it.

I'm not sure cause I don't see enough code.


[monkey][snake] <.
 
hi - thanks for your reply, got the combo
to submit the correct data but still need 'selected' after the selected option so my validation works.

i use combos all the time but most are populated from a database

Code:
<%
x_monthidList = "<select name='x_monthid'><option value=''>Month</OPTION>"
sqlwrk = "SELECT `monthid`, `month` FROM `month`"
Set rswrk = Server.CreateObject("ADODB.Recordset")
rswrk.Open sqlwrk, conn
If Not rswrk.Eof Then
    datawrk = rswrk.GetRows
    rowswrk = UBound(datawrk, 2)
    For rowcntwrk = 0 To rowswrk
        x_monthidList = x_monthidList & "<option value='" & datawrk(0, rowcntwrk) & "'"
        If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_titleid&"") Then
            x_monthidList = x_monthidList & " selected"
        End If
        x_monthidList = x_monthidList & ">" & datawrk(1, rowcntwrk) & "</option>"
    Next
End If
rswrk.Close
Set rswrk = Nothing
x_monthidList = x_monthidList & "</select>"
Response.Write x_monthidList
%>

my one is different because i dont have this line
Code:
        If CStr(datawrk(0, rowcntwrk)&"") = CStr(x_titleid&"") Then
            x_monthidList = x_monthidList & " selected"
        End If

how would you put this is the x_yearid combo
have added an if statement at the top to populate the combo
but my javascript validation wont work without 'selected'
next to the option

Code:
<%if x_yearid = "" then 
	  x_yearid ="Year"
	  end if
x_yearidList = "<select name='x_yearid'><option value=''>" & x_yearid & "</OPTION>"
currdate = date()
yearlimit = DateAdd("yyyy", -18, currdate)
yearlimit = (datepart("yyyy",yearlimit))
yearstart = yearlimit - 57

For x_yearid = yearstart to yearlimit
  x_yearidList = x_yearidList & "<option value='" & x_yearid & "'"
       'If CStr(x_yearid&"") = CStr(x_yearid&"") Then
       '     x_yearidList = x_yearidList & " selected"
       'End If
        x_yearidList = x_yearidList & ">" & x_yearid & "</option>"
    Next

x_yearidList = x_yearidList & "</select>"
Response.Write x_yearidList
%>
 
but my javascript validation wont work without 'selected'
next to the option

That's not true, you can give your <select> box an ID and reference the value with

Code:
document.getElementById("idNameGiven").value

This will grab the value that is currently selected in the dropdown, putting selected="selected" in the <option> is unecessary.

Can I see your Javascript validation function?




[monkey][snake] <.
 
thanks again monksnake - wasnt thinkin earlier, realised what i have to do

Code:
For i = yearstart to yearlimit
  x_yearidList = x_yearidList & "<option value='" & i & "'"
if CStr(i&"") = CStr(x_yearid&"") then 
    x_yearidList = x_yearidList & " selected"
End If
x_yearidList = x_yearidList & ">" & i & "</option>"
Next

although changing that function would have been a smarter way to do it.
 
I don't know what I did to help, but glad you got it working.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top